File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes  appropriate content type to download a zip file in a Struts App Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark " appropriate content type to download a zip file in a Struts App" Watch " appropriate content type to download a zip file in a Struts App" New topic
Author

appropriate content type to download a zip file in a Struts App

Vishal Dwivedi
Greenhorn

Joined: Jul 29, 2009
Posts: 2
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61603
    
  67

Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vishal Dwivedi
Greenhorn

Joined: Jul 29, 2009
Posts: 2
Thanks !! Although, I am not really 100% sure if this is a struts problem, or some issue with the way I am setting the response content type in my JSP page.

Bear Bibeault wrote:Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: appropriate content type to download a zip file in a Struts App