File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes JSP link to download a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP link to download a file" Watch "JSP link to download a file" New topic

JSP link to download a file

Linda Thomas
Ranch Hand

Joined: Jun 21, 2004
Posts: 36
I am trying to output the results of our database to a file and have the user download the file onto their computer. I have managed to get the file to be created but now on the confirmation page I want a link the user can click to download the file onto their machine where they would like to put it.

I've tried linking in Dreamweaver and it opens as an html page even tho the extension is .csv. I've tried right clicking and Save as but that also wants to save an html file.

Any suggestions how this can be done?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

If you want complete control over the file disposition you should write a servlet that will stream the file to the response after setting the appropriate content headers.

As such this has been moved to the Servlets forum.

[Asking smart questions] [About Bear] [Books by Bear]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

If you would like an example of this, I have one on my site.
Go to and download SimpleStream.
It streams images found under the WEB-INF directory but the principals are the same and it shows how to set both the contentType and contentDisposition tags to get the effect you want.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: JSP link to download a file
It's not a secret anymore!