开发者

Using JSP to download a file

开发者 https://www.devze.com 2023-01-22 05:06 出处:网络
I am currently trying to us开发者_开发百科e 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 abo

I am currently trying to us开发者_开发百科e 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号