• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to prevent file download dialog from appearing

Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


---code snippet----

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

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ServletOutputStream stream = response.getOutputStream();
int data;
while ((data = bis.read()) != -1) {
}catch(FileNotFoundException e){
}catch (Exception ex){
return mapping.findForward("fail");

Posts: 11962
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should check for the file's existence before doing anything with the response object. Have you tried that?
Clarence Dyho
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic