Applet/servlet communication for byte transmission
Hello all !
I wrote an applet to transfer binary file from web servlet (running under Tomcat 5.5) to a client (it's a signed applet) but I have a problem of interpretation of byte during transmission.
the code of the servlet is :
response.setContentType("application/octet-stream");
ServletOutputStream sos = response.getOutputStream();
FileInputStream fis =new FileInputStream(new File(
"C:\\WINDOWS\\system32\\setup.bmp"));
byte[] b =newbyte[1024];
int nbRead = 1;
while (nbRead > 0){
nbRead = fis.read(b);
System.out.println("octets lus = " + nbRead);
sos.write(b, 0, nbRead-1);
}
fis.close();
sos.close();
et le code de l'applet qui appelle cette servlet est :
URL selicPortal =null;
try{
selicPortal =new URL(
"http://localhost:8080/AppletTest/servlet/FileManipulation");
}catch (MalformedURLException e){
e.printStackTrace();
}
URLConnection selicConnection =null;
try{
selicConnection = selicPortal.openConnection();
}catch (IOException e){
e.printStackTrace();
}
selicConnection.setDoInput(true);
selicConnection.setDoOutput(true);
selicConnection.setUseCaches(false);
selicConnection.setRequestProperty("Content-Type",
"application/octet-stream");
try{
InputStream in = selicConnection.getInputStream();
FileOutputStream fos =new FileOutputStream(new File(tempDir
+"\\toto.bmp"));
byte[] b =newbyte[1024];
int nbRead = in.read(b);
while (nbRead > 0){
fos.write(b);
}
in.close();
fos.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
the file dowloaded is broken. it seems that bytes 01 00 or 00 01 are not correctly process.
Some ideas to help me please ?
[3363 byte] By [
sraulta] at [2007-10-3 5:21:16]

Why do you have setoutput to true? You are not sending anything to the
server. But when you set it to true you have to open and close the outputstream
on the urlconnection, try this: selicConnection.setDoOutput(false);
your applet tries to write to local filesystem, this can only be done by signed
and trusted applets or if the client has a special policy for your applet.
As for your servlet, I don't know. Try to open the url (http://localhost:8080/AppletTest/servlet/FileManipulation)
in a browser and save the file See if the file is saved correctly then you
know the servlet is OK.
Signing applets:
http://forum.java.sun.com/thread.jsp?forum=63&thread=524815
second post and reply 18 for the java class file using doprivileged
Still problems?
A Full trace might help us out:
http://forum.java.sun.com/thread.jspa?threadID=656028
hi,
have you solved this issue.. please post me the code since i m also doing the applet/servlet communication and can use your code as reference.
how to read the content placed in the urlConnection stream in the servlet
Below is my code in applet
public void upload(byte[] imageByte)
{
URL uploadURL=null;
try
{
uploadURL=new URL("<url>");
URLConnection urlConnection=uploadURL.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-type","application/octet-stream");
urlConnection.setRequestProperty("Content-length",""+imageByte.length);
OutputStream outStream=urlConnection.getOutputStream();
outStream.write(imageByte);
outStream.close();
}
catch(MalformedURLException ex)
{
}
catch(IOException ex)
{
}
How can i read the byte sent on the outstream (in above code) in the servlet.
Thanks,
Mclaren