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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
tom man
  • 11
  • 1
  • 1
  • 2
  • possible duplicate of [JSP page without HTML code for exporting data to Excel Sheet](http://stackoverflow.com/questions/1755509/jsp-page-without-html-code-for-exporting-data-to-excel-sheet) – BalusC Oct 23 '10 at 20:24
  • You'd get much better answers if you rephrase your question and be more specific about what advice you need – Yoni Oct 23 '10 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 – tom man Oct 23 '10 at 20:32
  • I removed the CSV ambiguity from your question since it's apparently not specifically about CSV files. – BalusC Oct 23 '10 at 21:21
  • Go through this link...It may help you.... http://stackoverflow.com/questions/13678094/what-should-be-the-content-type-to-download-any-file-format-in-jsp – loknath Dec 18 '12 at 06:26

1 Answers1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • that would be the millionth question about file download with servlets/jsp :) +1 anyway – Bozho Oct 23 '10 at 22:04