Solved

Send/Recv Zipped Data via HTTP

Posted on 2002-05-28
7
644 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

622 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