Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

odd jsp zip file return code

Status
Not open for further replies.

keak

Programmer
Sep 12, 2005
247
CA
Hi there,
I have the following code that basically reads in a zip file on the local machine and sends the bytes to the http request when a user requests it:
Code:
    response.setContentType("application/zip");
    response.setHeader ("Content-Disposition", "attachment;filename=\"test.zip\"");
    FileInputStream fis = new FileInputStream(filePath+"upnpLogs.zip");
    int c;
    while ( (c=fis.read()) != -1) {
        out.write(c);
    }
    fis.close();
    out.flush();

The code runs fine on one machine (uses Java 1.5.0_02-b09 with Linux: 2.6.9-5.ELsmp ), but running the exact same code at a different machine (Java 1.4.2 Linux:2.6.9-5.0.3.ELhugemem) returns me with a corruped Zip file; the zip program says "missing 492150 bytes in Zip file"

Is anyone aware of anything in the code that is perhaps causing this OS/Java dependent error?
 
Personally, I would say that code was inefficient. Why not use the inbuilt buffering ?

Code:
    response.setContentType("application/zip");
    response.setHeader ("Content-Disposition", "attachment;filename=\"test.zip\"");
    File f = new File(filePath+"upnpLogs.zip");
    byte[] data = new byte[(int)f.length()];
    FileInputStream fis = new FileInputStream(f);
    fis.read(data);
    fis.close();
    out.write(data);
    out.flush();

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks sedj for the post.

I am getting the following with the out.write function:
Code:
write(int) in the type Writer is not applicable for the arguments (byte[])

Is there another printing function that I need to use (since the zip content is a binary file), or should I be casting this byte array into an int?
 
Ahh, I take it that is in a JSP then, not a servlet.
This code would need to run in a servlet, because JSP's are not capable of writing byte[] data to the output stream.

(so out would be defined as response.getOutputStream() )

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top