tags:

views:

40

answers:

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?

+1  A: 

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
that would be the millionth question about file download with servlets/jsp :) +1 anyway
Bozho