Solved

Send/Recv Zipped Data via HTTP

Posted on 2002-05-28
7
627 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
reverse digits of a number using for loop 5 52
how to install java on RHEL image on EC2 4 40
ejb entity bean example 2 11
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

839 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