AGE_Nicolls
asked on
Send/Recv Zipped Data via HTTP
I am trying to write a Servlet that sends compressed data (such as XML) over HTTP to a Java client. Here is a simple code snippet that is not working. Anyone know why this fails?
executing the client code results in the following:
java.util.zip.ZipException : unknown compression method
========================== ========== ========== =
SERVER CODE
========================== ========== ========== =
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException,
IOException {
// imported java.util.zip.DeflaterOutp utStream
String message = "Hello world";
// Create DeflaterOutputStream that writes compressed
// data to ByteArrayOutputStream
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(mess age.length ());
DeflaterOutputStream compOut = new DeflaterOutputStream(byteO ut);
compOut.write(message.getB ytes());
compOut.close();
// write compressed bytes out to client
response.getOutputStream() .write(byt eOut.toByt eArray());
response.getOutputStream() .close();
}
========================== ========== ========== =
CLIENT CODE
========================== ========== ========== =
public static void main(String[] args) {
try {
// open connection to servlet that feeds us compressed data
// and read it into byte array
URL url = new URL("http://localhost:8080/ZipServlet/zipservlet?val=test");
URLConnection uConn = url.openConnection();
InputStream in = uConn.getInputStream();
byte[] bytes = new byte[in.available()];
// deflate using java.util.zip.InflaterInpu tStream
ByteArrayInputStream bis = new ByteArrayInputStream(bytes );
InflaterInputStream inflater = new InflaterInputStream(bis);
InputStreamReader in = new InputStreamReader(inflater );
char[] chars = new char[512];
StringBuffer buf = new StringBuffer(512);
int num = 0;
while ((num = in.read(chars)) > 0) {
buf.append(chars, 0, num);
}
String result = buf.toString();
System.out.println("DECOMP : " + result);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
executing the client code results in the following:
java.util.zip.ZipException
==========================
SERVER CODE
==========================
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException,
IOException {
// imported java.util.zip.DeflaterOutp
String message = "Hello world";
// Create DeflaterOutputStream that writes compressed
// data to ByteArrayOutputStream
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(mess
DeflaterOutputStream compOut = new DeflaterOutputStream(byteO
compOut.write(message.getB
compOut.close();
// write compressed bytes out to client
response.getOutputStream()
response.getOutputStream()
}
==========================
CLIENT CODE
==========================
public static void main(String[] args) {
try {
// open connection to servlet that feeds us compressed data
// and read it into byte array
URL url = new URL("http://localhost:8080/ZipServlet/zipservlet?val=test");
URLConnection uConn = url.openConnection();
InputStream in = uConn.getInputStream();
byte[] bytes = new byte[in.available()];
// deflate using java.util.zip.InflaterInpu
ByteArrayInputStream bis = new ByteArrayInputStream(bytes
InflaterInputStream inflater = new InflaterInputStream(bis);
InputStreamReader in = new InputStreamReader(inflater
char[] chars = new char[512];
StringBuffer buf = new StringBuffer(512);
int num = 0;
while ((num = in.read(chars)) > 0) {
buf.append(chars, 0, num);
}
String result = buf.toString();
System.out.println("DECOMP
}
catch (Exception ex) {
ex.printStackTrace();
}
}
ASKER
That didn't do it - but thanks...
Are you still with DMCI? Earlier today I checked your profile for your email address, and noticed you worked there. I sent you an email but wasn't sure if you'd get it if you were no longer with them.
I'd like to catch up on what's going on there. Shoot me an email at matt_nicolls@mastercard.co m if you have a second.
-Matt
Are you still with DMCI? Earlier today I checked your profile for your email address, and noticed you worked there. I sent you an email but wasn't sure if you'd get it if you were no longer with them.
I'd like to catch up on what's going on there. Shoot me an email at matt_nicolls@mastercard.co
-Matt
Not sure you should be wrapping your stream in a Reader?
Reader's are intended for reading character data, and your servlet is writing a byte array.
Though I wouldn't think this would create your problem.
What version of Java is running at each end?
Reader's are intended for reading character data, and your servlet is writing a byte array.
Though I wouldn't think this would create your problem.
What version of Java is running at each end?
Your client doesn't actually read from the connection stream, it attempts to read from the (empty) ByteArrayInputStream.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good job objects. Thanks.
-Matt
-Matt
No worries :-)
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
response.setContentType("a
Jim