Solved

Send/Recv Zipped Data via HTTP

Posted on 2002-05-28
7
607 Views
Last Modified: 2008-02-01
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();
  }
}
0
Comment
Question by:AGE_Nicolls
  • 4
  • 2
7 Comments
 
LVL 19

Expert Comment

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

Jim
0
 
LVL 1

Author Comment

by:AGE_Nicolls
ID: 7040561
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
0
 
LVL 92

Expert Comment

by:objects
ID: 7040583
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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 7040743
Your client doesn't actually read from the connection stream, it attempts to read from the (empty) ByteArrayInputStream.
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 7040750
TRy something like:

===============================================
SERVER CODE
===============================================

public void doGet(HttpServletRequest request,
                 HttpServletResponse response)
  throws ServletException, IOException {
 String message = "Hello world";

 DeflaterOutputStream compOut = new DeflaterOutputStream(response.getOutputStream());
 OutputStreamWriter out = new OutputStreamWriter(compOut);

 out.write(message);
 out.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();
   InflaterInputStream inflater = new InflaterInputStream(in);
   BufferedReader in = new BufferedReader(
     new InputStreamReader(inflater));
   result = in.readLine();
   System.out.println("DECOMP: " + result);
 }
 catch (Exception ex) {
   ex.printStackTrace();
 }
}
0
 
LVL 1

Author Comment

by:AGE_Nicolls
ID: 7041757
Good job objects.  Thanks.

-Matt
0
 
LVL 92

Expert Comment

by:objects
ID: 7043244
No worries :-)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now