Solved

Send/Recv Zipped Data via HTTP

Posted on 2002-05-28
7
598 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

706 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

17 Experts available now in Live!

Get 1:1 Help Now