简体   繁体   English

如何下载文件而不是在浏览器中打开?

[英]How to make files download instead of opening in browser?

I am a new bie, i want files to get downloaded when user clicks on download option its opening in the browser instead of download option like save as/open.Here i referred for the same and every where they have suggested to use 我是一个新的bie,我希望文件在用户点击下载选项时下载它在浏览器中打开而不是下载选项如save as / open.Here我引用了相同的以及他们建议使用的每个地方

Response.AddHeader("Content-disposition", "attachment; filename=" + Name);

But i don't know where and how to use. 但我不知道在哪里以及如何使用。 Actually i'm getting the url value from query written which return url as one of the object of bean stored in arraylist(This list is having other values also with url ). 实际上我从查询中获取url值,返回url作为存储在arraylist中的bean的对象之一(此列表还包含其他值与url)。 I am having the url values inside the arraylist as bean as like 我在arraylist中的url值就像bean一样

type=.pdf
release date=12/3/08
name=hai.pdf
url=/files/en/soft/doc/docs/hai.pdf

I am getting this array list in my controller like this 我在我的控制器中得到这个数组列表

ArrayList details = dao.getdetails(Bean.getNumber());

and pass this into view like this 并将此传递到视图中

Map.put("details", details);
modelView.setViewName("details_list");
modelView.addAllObjects(Map);
return modelView;

in jsp i have iterated this array list and diplays the content like this 在jsp中,我迭代了这个数组列表并像这样显示内容

Type    name            Release Date            
.txt    hai.pdf     May 21st 2012   Download

.txt    hello.txt   May 21st 2012   Download

For download i have used like this in jsp 对于下载,我在jsp中使用过这样的东西

<td colspan="2" valign="top">                           
<a href="${details.Url}"/>
<img src="/images/download.gif" alt="Download" border="0" align="right"></a>
</td>

here on click of download its opening in browser.I need this to be downloaded instead. 点击下载在浏览器中打开它。我需要下载它。 Please help me in how to use or handle 请帮助我如何使用或处理

response.setHeader("Content-Disposition", "attachment;");

where to add the above for my requirement or if i can do with any java script also.Please help me in solving the above. 在哪里添加上面的我的要求或如果我也可以使用任何java脚本。请帮助我解决上述问题。

Here is one way of doing it: 这是一种方法:

  1. Create a web Filter (or this way ) 创建一个Web 过滤器 (或这种方式
  2. Map this filter to the PDF URL. 将此过滤器映射到PDF URL。
  3. In the doFilter() method, set the response header for content download. doFilter()方法中,设置内容下载的响应头。

Example : 示例:

public void doFilter(ServletRequest request, 
   ServletResponse response, FilterChain chain)
   throws IOException, ServletException {

          String name = request.getAttribute("filename");

           response.addHeader("Content-disposition", "attachment; filename=" + name);
           chain.doFilter(request, response);


}

You can set the filename as an request attribute (reqest.setAttribute()) from your controller class 您可以将文件名设置为来自控制器类的请求属性(reqest.setAttribute())

Filters are pretty standard in Java web stack. 过滤器在Java Web堆栈中非常标准。

It's depend on the header which browser get in response. 这取决于浏览器响应的标题。

Suppose if header is image/png then browser will show it. 假设如果header是image / png,那么浏览器将显示它。 Same way if you send same image with application/octet-stream then browser will force to download it. 如果您使用application / octet-stream发送相同的图像,浏览器将强制下载它。

take a look http://en.wikipedia.org/wiki/Byte_stream 看看http://en.wikipedia.org/wiki/Byte_stream

In a project I have figure out that sending request from browser are different. 在一个项目中,我发现从浏览器发送请求是不同的。

If you upload a image from Firefox or IE then it's will upload them as image/png wherever chrome upload them as application/octet-stream. 如果您从Firefox或IE上传图像,那么它会将它们上传为image / png,无论Chrome上传它们作为application / octet-stream。

just try to add header 只是尝试添加标题

response.setHeader("Content-Type: application/force-download"); response.setHeader(“Content-Type:application / force-download”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让Safari下载CSV而不是在浏览器中显示? - How can I make Safari download a CSV instead of showing it in the browser? 如何从客户端计算机下载zip文件,而不是使用Java在浏览器中打开文件? - how to download zip-file from client machine instead of opening file in browser using java? 下载文件,而不是使用struts2在浏览器中打开 - download file instead of opening in browser using struts2 页面正在浏览器而不是Webview上打开 - page is opening on browser instead of Webview 如何阻止webChromeClient在Android浏览器中打开文件 - How can I stop webChromeClient from opening files in the Android Browser 在浏览器中打开PDF文件易受攻击 - Opening PDF files in the browser vulnarability 如何使JSP页面下载2个文件 - How to make a jsp page download 2 files 如何使pdf显示为下载选项而不是在浏览器上呈现? - How to make a pdf appear as a download option rather than rendering on the browser? 如何在testng for selenium中使浏览器的打开(在一个浏览器中全部测试)静态 - How to make the opening of browser(all test in one browser) static in testng for selenium android应用程序正在打开浏览器应用程序而不是webView,我该如何停止呢? - android application is opening a browser application instead of a webView, how do I stop that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM