Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

java.io.StreamCorruptedException: invalid stream header

843841Oct 5 2004 — edited Oct 5 2004
Hi,
I created an example with an applet and a servlet. The applet send an object to the servlet and the servlet send the same object with some modifies in its data.
Now the problems is : when the applet read the object sent by servlet i get this exception : java.io.StreamCorruptedException: invalid stream header, why ?

This is the code the applet:
----------------------------------------------------------------------------------------------------------------
// send data to servlet
// obj is the object to send
private void inviaDati(Socket s,DatiApplet obj)
throws IOException
{
ObjectOutputStream out = null;
DataOutputStream dataout = null;
ByteArrayOutputStream buffer = null;
StringBuffer sb = null;
byte[] serobj = null;

buffer = new ByteArrayOutputStream();
out = new ObjectOutputStream(buffer);
out.writeObject(obj);

serobj = buffer.toByteArray();
sb = new StringBuffer();
sb.append("POST ");
sb.append(percorsoServlet);
sb.append(" HTTP/1.0\r\nUser-Agent: JavaObjectTunnel\r\n");
sb.append("Content-Type: application/java-object\r\n");
sb.append("Content-Length: ");
sb.append(serobj.length);
sb.append("\r\n\r\n");

dataout = new DataOutputStream(s.getOutputStream());
dataout.write(sb.toString().getBytes());
dataout.write(serobj);
dataout.write("\r\n".getBytes());
}

// read data from servlet
private DatiApplet leggiDati(Socket s)
throws StreamCorruptedException,ClassNotFoundException,IOException
{
DatiApplet obj = null;
ObjectInputStream in = null;

in = new ObjectInputStream(new DataInputStream(s.getInputStream()));
obj = (DatiApplet)in.readObject();
return obj;
}
----------------------------------------------------------------------------------------------------------------

This is the code the servlet:
----------------------------------------------------------------------------------------------------------------
// read data from applet
// return object sent
private DatiApplet leggiDatiDaApplet(HttpServletRequest request)
throws StreamCorruptedException,ClassNotFoundException,IOException
{
DatiApplet obj = null;
ObjectInputStream in = null;

in = new ObjectInputStream(new DataInputStream(request.getInputStream()));
obj = (DatiApplet)in.readObject();

return obj;
}

private void inviaDatiVersoApplet(HttpServletResponse response,DatiApplet obj)
throws IOException
{
ObjectOutputStream out = null;
DataOutputStream dataout = null;
ByteArrayOutputStream buffer = null;
byte[] serobj = null;

buffer = new ByteArrayOutputStream();
out = new ObjectOutputStream(buffer);
out.writeObject(obj);

serobj = buffer.toByteArray();
response.setContentType("application/*");
response.setContentLength(serobj.length);
dataout = new DataOutputStream(response.getOutputStream());
dataout.write(serobj);
}
----------------------------------------------------------------------------------------------------------------

Thanks
Fulvia

Comments

843841
can you use HttpUrlConnection
instead of sockets or can you only
use sockets?
843841
Ok thanks, i used HttpUrlConnection instead of Socket, the application works well.
Thanks a lot

Fulvia
1 - 2
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 2 2004
Added on Oct 5 2004
2 comments
263 views