Link to home
Start Free TrialLog in
Avatar of AGE_Nicolls
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.DeflaterOutputStream    

  String message = "Hello world";
       
  // Create DeflaterOutputStream that writes compressed
  // data to ByteArrayOutputStream
  ByteArrayOutputStream byteOut = new ByteArrayOutputStream(message.length());
  DeflaterOutputStream compOut = new DeflaterOutputStream(byteOut);
  compOut.write(message.getBytes());
  compOut.close();

       
  // write compressed bytes out to client
  response.getOutputStream().write(byteOut.toByteArray());
  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.InflaterInputStream
    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();
  }
}
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Not sure, but try adding this before getting the OutputStream:
    response.setContentType("application/zip");

Jim
Avatar of AGE_Nicolls
AGE_Nicolls

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.com if you have a second.

-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?
Your client doesn't actually read from the connection stream, it attempts to read from the (empty) ByteArrayInputStream.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good job objects.  Thanks.

-Matt