Hi !!
I am trying to download a zip file through a
JSP page. The JSP page is a tiled one, and part of a Struts-1 application. The zipping of files goes perfectly fine for me, but I am finding it hard to open a window to store the zip file (which should be done by default), as the entire zipped file text is copied on to the next window (which appears as a garbage text). After struggling through out the day, I guess that one reason could be that my
struts application is not accepting the appropriate response type as application/zip, and instead inlining it as text/html.
Some other things which might be important for this problem:
-------
1) The exact code fragment of my JSP is as follows:
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment;filename=\"new.zip\""); //the name of zip file I create
out.clearBuffer();
ZipOutputStream zipout = new ZipOutputStream(new Writer2Stream(out));
zipout.setComment("The zipped files");
zipout.setLevel(COMPRESSION_LEVEL);
for (int i = 0; i < v.size(); i++) { //Vector v stores the list of files
File f = (File) v.get(i);
if (f.canRead()) { // just a small permission check
zipout.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(dir_l + 1)));
BufferedInputStream fr = new BufferedInputStream(new FileInputStream(f));
byte buffer[] = new byte[0xffff];
copyStreamsWithoutClose(fr, zipout, buffer); // a small function which copies the bytestream to zip stream
int b;
while ((b=fr.read())!=-1) zipout.write(b);
fr.close();
zipout.closeEntry();
}
}
zipout.finish();
out.flush();
The zip file is actually getting created, but instead of a save-as popup window, the entire zip file appears in text form in the next JSP, which looks something like this:
PK���Ͼý:������������ ���arrow1.gifs÷t³°Läfàf˜øŸáÿÿÿ—/_f``8pà€âOFf (ËÍàÀÄ7§¥Ä]îÈ9—#Ÿ®°ce°�PKŠ+?���=���PK���Ͼý:������������ ���arrow2.gifs÷t³°Läcàd˜øŸáÿÿÿ—/_f``8pà€âOFf —$ïÀ$8§åì‡{ºÓ/1êoûxwƒ5�PKõhr!A���@���PK����Ͼý:Š+?���=��� �����������������arrow1.gifPK����Ͼý:õhr!A���@��� �������������w���arrow2.gifPK������p���ð���"�The zipped files
Although the above text is the appropriate zip, its just that its opened in text format. It is not only for this zip file, but other files also get in-lined instead of being saved, even though I provide a proper response.setContentType parameter based on their mime type.
------
2) This JSP is a used as a tiled page, whose logic goes something like this
<logic:equal value="" parameter="parameter1">
<tiles:insert template="/mainPage.jsp" /> //this mainpage has a left menu, header, footer and a central page where everything works perfectly fine
</logic:equal>
------
3) My request processor entry in struts-config file is following (I am using struts1):
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
------
The JSP page is an adaptation of the JSP brwoser by
http://www.vonloesch.de, which works perfectly fine as a standalone JSP; but the moment I use this JSP as a tiled page in my struts application with some minor modifications, the response types just get screwed up.
Is there something which I am missing in setting the responseContentType? Or this is some bug with struts1/tiles? I would be really thankful to anyone who can help me to resolve the problem.
Regards,
Vishal