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 How to prevent file download dialog from appearing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to prevent file download dialog from appearing" Watch "How to prevent file download dialog from appearing" New topic
Author

How to prevent file download dialog from appearing

Clarence Dyho
Greenhorn

Joined: Sep 17, 2001
Posts: 23
Hello,

My JSP contains a link to download a CSV file from the server. The JSP path and filename are stored in the ApplicationResources properties file and is resolved at runtime. Clicking this link calls an Action that handles the download. When the file to be downloaded does not exist, the try-catch block returns a FileNotFoundException. My app works this far and the logs show that I do handle the exception.

My problem is that the file download box (the one with open/save/cancel button) still appears even when the file does not exist on the server. Clicking on "save" will save a new, empty file with the same filename of the file to be downloaded. "Open" opens a new file with the same name.

Another problem is that I cannot forward to the error page where the errors would appear in the <html:errors/> section. It's like the Download dialog prevents this from happening.

I appreciate your inputs on this. Thank you very much.

Regards,
Clarence

---code snippet----
...

File file = new File(path+filename);
response.setContentType("application/unknown");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
long fileLength = file.length();
response.setHeader("Content-Length", new Long(fileLength).toString());

try{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ServletOutputStream stream = response.getOutputStream();
int data;
while ((data = bis.read()) != -1) {
stream.write(data);
}
bis.close();
stream.flush();
stream.close();
}catch(FileNotFoundException e){
...
}catch (Exception ex){
...
}
saveErrors(request,errors);
if(errors.size()!=0){
return mapping.findForward("fail");
}

...
-------------------
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I think you should check for the file's existence before doing anything with the response object. Have you tried that?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Clarence Dyho
Greenhorn

Joined: Sep 17, 2001
Posts: 23
Thanks for you input.

Now that was embarassing. I'm shocked as to why I didn't think about that in the first place. I think I need a vacation

Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to prevent file download dialog from appearing