Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

downloading from a servlet

 
Adewale Adebusoye
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please tell me how to download files from a servlet? Im talking about those "pop-ups " like JFileChooser that allow you to specify the directory you want to download the file to.
Thanks
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your servlet reponse, you set HTTP headers like this:

response.setHeader("Content-Type", "text/plain");
response.setHeader("Content-Disposition", "attachment; filename=file.txt");

The first header indicates what kind of MIME type the file is you're streaming to the client, while the second specifies that the file should not be displayed by the browser, but instead should be stored as a file. You can change the suggested filename and use something more descriptive than "file.txt".
[ January 27, 2006: Message edited by: Ulf Dittmer ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adewale Adebusoye:
Hello:
i included the servlet response headers:

response.setHeader("Content-Type", "text/plain");
response.setHeader("Content-Disposition", "attachment; filename=file.txt"));

the only thing that its not downloading my attached file, rather, its set the filename to null, and its downloading the dynamic html generated by my servlet into a file.

So am i supposed to actually code for the download itself?

Please reply


Yes.
Fortunately, this is not too difficult:

Example:
 
Adewale Adebusoye
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please the part of the code that has getServletContext.getResourceAsStream("Web.inf",filename) is a bit confusing.
What is web-inf doing there? is that where the resource is meant to be stored?
im sorry if im asking a silly question, but ive never seen that before.
thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This servlet is part of a demo app that streams image files from behind the WEB-INF directory.

The getResourceAsStream method takes as it's argument the path of the file that you want to work with, relative to the root of your webapp.

In this case, I'm prepending "/WEB-INF" to the user supplied file name.

See:
java.lang.String)" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)
for more information.
 
Adewale Adebusoye
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please the part of the code that has getServletContext.getResourceAsStream("Web.inf",filename) is a bit confusing.
What is web-inf doing there? is that where the resource is meant to be stored?
im sorry if im asking a silly question, but ive never seen that before.
thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you not read Ben's reply to the first time you asked this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic