压缩流问题
在Applet中通过流获取后台的对象时出错,请问可能是哪里出错了
java.io.IOException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
at java.util.zip.GZIPInputStream. <init> (Unknown Source)
at java.util.zip.GZIPInputStream. <init> (Unknown Source)
代码如下:
URLConnection uc = new URLConnection( "http://test/test.do ");
GZIPInputStream gIn = new GZIPInputStream(uc.getInputStream());
ObjectInputStream in = new ObjectInputStream(gIn);
Object o = in.readObject();
gIn.close();
in.close();
------解决方案--------------------因为uc返回来的流根本不是GZIP的输入流,所以你不应该使用GZIP来封装它,直接ObjectInputStream就可以了
------解决方案--------------------很明显,压缩文件的格式不对,GZIPInputStream解不了
------解决方案--------------------应该是可以的,可能是你服务器test.do写得不正确
t.jsp:
<%@ page contentType= "text/html; charset=GBK "%> <%
java.util.zip.GZIPOutputStream gOut = new java.util.zip.GZIPOutputStream(response.getOutputStream());
java.io.ObjectOutputStream output = new java.io.ObjectOutputStream(gOut);
output.writeObject( "Object ");
output.flush();
output.close();
%>
Client:
ObjectInputStream ois = new ObjectInputStream(new GZIPInputStream(new URL( "http://localhost:8080/t.jsp ").openStream()));
System.out.println(ois.readObject());
可以正确返回