osa
01-06-2007, 16:58
Salve ho un client (dovrebbe essere un applet) che deve inviare un file binario (non si tratta di un file testuale, ma può essere un immagine, un file zippato, ecc) ad una servlet, la quale riceve lo stream di dati e lo scrive sul disco in un file.
Il problema è che il client si collega alla servlet ma non accade nulla, so di certo che il client legge il file lo invia ma la servlet non compie azioni. Perchè?
Ho creato la servlet cosi:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
System.out.println("----> Ok Collegato");
res.setContentType("text/plain");
ServletInputStream fromApplet = req.getInputStream();
// creo file
File ff=new File("/home/pippo/Desktop/prova");
ff.createNewFile();
FileOutputStream fw= new FileOutputStream(ff);
System.out.println("---> Inizio lettura");
int rb;
byte[] buff=new byte[200];
while ( ( rb=fromApplet.read(buff) ) !=-1 ) //leggo dal client
{
fw.write(buff,0,rb); //scrivo nel file
}
fw.close();
fromApplet.close();
}
Questo è il codice del client:
public ConnectionServlet(String url) throws MalformedURLException, IOException {
byte[] buff=new byte[100];
URL servletURL = new URL("http://localhost:8080/servlet/UploadServletSEG");
URLConnection servletConnection = servletURL.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
servletConnection.setDoInput(true);
OutputStream toServlet=servletConnection.getOutputStream();
FileInputStream fr = new FileInputStream("/home/pippo/Desktop/prova_read"); //apro file
int rb;
FileOutputStream fw= new FileOutputStream(ff);
while ( (rb=fr.read(buff)) != -1 )
{
toServlet.write(buff,0,rb); //invio alla servlet
System.out.println("Lettura"+rb);
}
fr.close();
toServlet.close();
Il problema è che il client si collega alla servlet ma non accade nulla, so di certo che il client legge il file lo invia ma la servlet non compie azioni. Perchè?
Ho creato la servlet cosi:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
System.out.println("----> Ok Collegato");
res.setContentType("text/plain");
ServletInputStream fromApplet = req.getInputStream();
// creo file
File ff=new File("/home/pippo/Desktop/prova");
ff.createNewFile();
FileOutputStream fw= new FileOutputStream(ff);
System.out.println("---> Inizio lettura");
int rb;
byte[] buff=new byte[200];
while ( ( rb=fromApplet.read(buff) ) !=-1 ) //leggo dal client
{
fw.write(buff,0,rb); //scrivo nel file
}
fw.close();
fromApplet.close();
}
Questo è il codice del client:
public ConnectionServlet(String url) throws MalformedURLException, IOException {
byte[] buff=new byte[100];
URL servletURL = new URL("http://localhost:8080/servlet/UploadServletSEG");
URLConnection servletConnection = servletURL.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
servletConnection.setDoInput(true);
OutputStream toServlet=servletConnection.getOutputStream();
FileInputStream fr = new FileInputStream("/home/pippo/Desktop/prova_read"); //apro file
int rb;
FileOutputStream fw= new FileOutputStream(ff);
while ( (rb=fr.read(buff)) != -1 )
{
toServlet.write(buff,0,rb); //invio alla servlet
System.out.println("Lettura"+rb);
}
fr.close();
toServlet.close();