aspose file tools*
The moose likes Servlets and the fly likes downloading from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "downloading from a servlet" Watch "downloading from a servlet" New topic
Author

downloading from a servlet

Adewale Adebusoye
Ranch Hand

Joined: Sep 28, 2005
Posts: 118
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


"Human beings can alter their lives by altering their state of mind" William James
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
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 ]

Ping & DNS - my free Android networking tools app
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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:


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Adewale Adebusoye
Ranch Hand

Joined: Sep 28, 2005
Posts: 118
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Sep 28, 2005
Posts: 118
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

Joined: Jan 10, 2002
Posts: 61224
    
  66

Did you not read Ben's reply to the first time you asked this?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: downloading from a servlet