Solved

Send/Recv Zipped Data via HTTP

Posted on 2002-05-28
7
618 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
simple java question 3 56
ForLoop Example 3 49
Android development question 2 37
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

778 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