使文档可通过java / servlet下载 [英] make document available for download through java/servlet

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

问题描述

我需要知道java / servlet中是否存在一种存储在数据库中的文档(doc,pdf)可以以请求的方式下载到用户的方式(请参见下文),

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

一个servlet url处理负责提取文档表单数据库并提供给用户下载的文档的下载

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

感谢您的时间和精力

推荐答案

您需要在您的servlet中添加以下标题,以使其成为可下载的内容,因此浏览器不会尝试显示。

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.

这篇关于使文档可通过java / servlet下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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