使用JSP下载文件 [英] Using JSP to download a file

查看:113
本文介绍了使用JSP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用JSP构建一些小型的litle应用程序,并且没有陷入困境,从Web服务器下载文件。我似乎无法弄清楚我应该如何完成这项任务。

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.

这里是否有任何JSP开发人员知道如何解决这个问题并指出我正确的方向?

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

推荐答案

如果资源是静态的,只需将其放入公共webcontent(您的JSP / HTML / CSS / JS所在的位置) / etc文件也是)并在JSP中包含指向它的链接。

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>

servletcontainer会担心设置正确的HTTP响应标头。

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

如果资源是动态的,创建一个servlet,以某种方式获取内容的 InputStream new FileInputStream resultSet.getBinaryStream(),etc..etc ..)并将其写入 OutputStream 的至少响应 Content-Type Content-Disposition 响应标头。最后,只需链接到JSP中的servlet。

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>

您可以在这篇文章

Content-Type 标题通知客户端文件的内容类型,以便它知道应该使用什么应用程序来打开它。 Content-Disposition 标题通知客户端如何处理,内联显示或保存为附件。

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.

这篇关于使用JSP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆