Deserialize a Java object which I get from a JSP page?
JSP code开发者_如何转开发:
<%
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(stream);
output.writeObject(new BigDecimal("111"));
output.flush();
output.close();
response.getOutputStream().write(stream.toByteArray());
out.clear();
out = pageContext.pushBody();
%>
code at client, (response is a object of org.apache.http.HttpResponse
)
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
byte[] buff = new byte[1024];
is.read(buff);
ByteArrayInputStream bi = new ByteArrayInputStream(buff);
ObjectInputStream oi = new ObjectInputStream(bi);
I get the exception:
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 3C68746D
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
ObjectOutputStream oos = null;
HttpURLConnection uc = null;
try
{
URL url = new URL("http://12.13.152.12:7077/client.jsp");
uc= (HttpURLConnection) url.openConnection();
uc.setRequestMethod("POST");
uc.setUseCaches (false);
uc.setDefaultUseCaches(false);
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setRequestProperty ("Content-Type","application/octet-stream;charset=UTF-8");
uc.connect();
oos = new ObjectOutputStream(uc.getOutputStream());
oos.writeObject(new String("hello"));
}
catch(Exception ee)
{
}
finally
{
oos.flush();
oos.close();
oos = null;
uc.disconnect();
}
**in client side**
*client.jsp*
InputStream inputStream = null;
ObjectInputStream ois = null;
try
{
inputStream = request.getInputStream();
ois = new ObjectInputStream(inputStream);
String companyName`enter code here`="";
String temp = (String)ois.readObject();
System.out.println("temp ");
}
Could be somewhere else the data is being written directly to output stream as bytes. The response stream has mix of object stream and bytes . Which fails during decoding because you are treating the entire content as objectstream.
// JAVA code uses Apache HttpClient
public void testObjectStream() {
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:8084/test_serialize.jsp");
httpPost.setEntity(new SerializableEntity(new String("testing 123"), false));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
ObjectInputStream ois = new ObjectInputStream(content);
System.out.println("obj : " + (String)ois.readObject());
} else {
System.out.println("failed : " + statusCode);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
// JSP page (DO NOT ADD LINE BREAKS or the JSP will take control of the outputstream before you can)
// test_serialize.jsp - This JSP will read an object from the inputstream (a String in our test) and write it back to the outputstream
<%@ page import="java.util.*, java.io.*" %><%
InputStream inputStream = request.getInputStream();
ObjectInputStream ois = new ObjectInputStream(inputStream);
String temp = (String)ois.readObject();
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(new String(temp));
%>
精彩评论