使文档可通过 java/servlet 下载
我需要知道java/servlet中是否有一种方法可以使存储在数据库中的文档(doc,pdf)可供用户按请求的方式下载(请参见下文),
例如有一个网页和文档的链接其中
现在是这样完成的: 如果用户单击该链接,则会打开一个新的空白窗口,并显示下载对话框,用户可以下载文档,但该空白窗口保持打开状态 用户必须手动关闭它
但希望这样做: 如果用户单击该链接而不是直接停留在该页面上,则会显示一个下载对话框,要求他们保存文件,
servlet url 会处理文档的下载,该文档负责提取文档表单数据库并可供用户下载
感谢您的时间和精力
I need to know if there is a way in java/servlet to make documents(doc,pdf) stored in database available for download to users in requested way(please see below),
for example there is a web page and the link for document in it
right now it is done this way:
if the user clicks that link than a new blank window opens and there the download dialog box is shown and the user is able to download the document but that blank window stays open
and the user have to close it manually
but wish to do it this way:
If the User clicks that link than directly staying on that page a download dialog box should show up asking them to save the file
a servlet url handles the download of the document which is responsible for extracting the doc form database and makes available for download to users
thank you for your time and effort
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在 servlet 中添加以下标头,使其成为可下载内容,以便浏览器不会尝试显示它,
文件名是建议的文件名,用户可以更改它。
You need to add following headers in your servlet to make it a downloadable content so browsers don't try to display it,
The filename is proposed filename and user can change it.
我想知道你的链接 html 是否没有类似的内容:
否则,它应该按你想要的方式工作。
I wonder if your link html doesn't have something like:
Otherwise, it should work as you want.
这是 IE 中的一个错误,它取决于几个因素,内容类型就是其中之一。几年前我们遇到了同样的问题,但我不记得正确的解决方案了,只记得我们为此奋斗了相当长一段时间。试试这个:
application/pdf
)application/octet-stream
),这应该告诉IE 不理会该文件。不过,您可能会遇到文件扩展名的问题。其中之一使 IE 表现良好。祝你好运。
This is a bug in IE which depends on several things, the content type is one of them. We had the same problem a few years ago but I don't remember the correct solution anymore, only that we struggled with this for quite some time. Try this:
application/pdf
)application/octet-stream
) which should tell IE to leave the file alone. You may have problems with the file extension, though.One of these things made IE behave. Good luck.
您需要从
元素中删除
target="_blank"
。编辑:根据您的评论:您需要将
Content-Disposition
标头设置为附件
。您可以在此处找到 简单 fileservlet 和 高级 fileservlet 以获得一些见解。You need to remove
target="_blank"
from your<a>
element.Edit: as per your comments: you need to set
Content-Disposition
header toattachment
. You can find here examples of a simple fileservlet and an advanced fileservlet to get some insights.