使文档可通过 java/servlet 下载

发布于 2024-08-13 12:27:46 字数 344 浏览 8 评论 0原文

我需要知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韵柒 2024-08-20 12:27:46

您需要在 servlet 中添加以下标头,使其成为可下载内容,以便浏览器不会尝试显示它,

String value = "attachment;filename=\"" + URLEncoder.encode(filename, "UTF-8") +'"';
response.setHeader("Content-Disposition", value);
response.setHeader("Content-Transfer-Encoding", "binary");

文件名是建议的文件名,用户可以更改它。

You need to add following headers in your servlet to make it a downloadable content so browsers don't try to display it,

String value = "attachment;filename=\"" + URLEncoder.encode(filename, "UTF-8") +'"';
response.setHeader("Content-Disposition", value);
response.setHeader("Content-Transfer-Encoding", "binary");

The filename is proposed filename and user can change it.

眼睛会笑 2024-08-20 12:27:46

我想知道你的链接 html 是否没有类似的内容:

<a href="/foo" **target="_blank"** ....>download</href>

否则,它应该按你想要的方式工作。

I wonder if your link html doesn't have something like:

<a href="/foo" **target="_blank"** ....>download</href>

Otherwise, it should work as you want.

巨坚强 2024-08-20 12:27:46

这是 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:

  • Use the correct content type (application/pdf)
  • If that doesn't work, use a wrong file type (like application/octet-stream) which should tell IE to leave the file alone. You may have problems with the file extension, though.
  • Send or don't send the correct file size
  • Check which chunking mode you're using.

One of these things made IE behave. Good luck.

墨小沫ゞ 2024-08-20 12:27:46

您需要从 元素中删除 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 to attachment. You can find here examples of a simple fileservlet and an advanced fileservlet to get some insights.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文