1

I am currently trying to use JSP to build some small litle apps and have not got stuck on something, downloading files from a webserver. I just cant seem to work out how I should go about this task.

Are there any JSP developers here who know to go about this and could point me in the right direction?

5
  • possible duplicate of JSP page without HTML code for exporting data to Excel Sheet Commented Oct 23, 2010 at 20:24
  • You'd get much better answers if you rephrase your question and be more specific about what advice you need Commented Oct 23, 2010 at 20:26
  • What I mean is, what classes would be used to download files, Its not necessarily a CSV file, it could be anything, But how do i go about downloading files from a server using Java Commented Oct 23, 2010 at 20:32
  • I removed the CSV ambiguity from your question since it's apparently not specifically about CSV files. Commented Oct 23, 2010 at 21:21
  • Go through this link...It may help you.... stackoverflow.com/questions/13678094/… Commented Dec 18, 2012 at 6:26

1 Answer 1

7

If the resource is static, just put it in the public webcontent (there where your JSP/HTML/CSS/JS/etc files also are) and include a link to it in your JSP.

<a href="file.ext">download</a> 

The servletcontainer will worry about setting the right HTTP response headers.

If the resource is dynamic, create a servlet which obtains an InputStream of the content somehow (new FileInputStream, resultSet.getBinaryStream(), etc..etc..) and writes it to the OutputStream of the response along at least the Content-Type and Content-Disposition response headers. Finally just link to that servlet in your JSP.

<a href="fileservlet/file.ext">download</a> 

You can find a basic example in this article.

The Content-Type header informs the client about the content type of the file so that it knows what application it should use to open it. The Content-Disposition header informs the client what to do with it, displaying it inline or saving as attachment.

Sign up to request clarification or add additional context in comments.

1 Comment

that would be the millionth question about file download with servlets/jsp :) +1 anyway

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.