如何使文件下载,而不是在浏览器中打开? [英] How to make files download instead of opening in browser?

查看:518
本文介绍了如何使文件下载,而不是在浏览器中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的朋友,我希望文件下载,当用户点击下载选项在浏览器中打开,而不是下载选项,如保存为/ open.Here我引用了相同和他们建议使用的每个地方

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

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

但我不知道在哪里和如何使用。
实际上,我从查询中获取url值,返回url作为存储在arraylist中的bean的对象之一(此列表还具有url的其他值)。
我将数组列表中的url值作为bean,就像

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 ). I am having the url values inside the arraylist as bean as like

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;

在jsp我已经迭代了这个数组列表,并显示这样的内容

in jsp i have iterated this array list and diplays the content like this

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

.txt    hello.txt   May 21st 2012   Download

下载我已经在jsp中使用了这样的东西

For download i have used like this in 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;");

在哪里添加以上为我的要求,或者如果我可以做任何java脚本也请。帮助

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.

推荐答案

这是一种做法:


  1. 创建一个网站过滤器 (或此方式

  2. 将此过滤器映射到PDF URL。

  3. doFilter()方法中,设置内容下载的响应标头。

  1. Create a web Filter (or this way )
  2. Map this filter to the PDF URL.
  3. In the doFilter() method, set the response header for content download.

示例:

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);


}

您可以将文件名设置为请求属性(reqest.setAttribute())从您的控制器类

You can set the filename as an request attribute (reqest.setAttribute()) from your controller class

过滤器在Java Web栈中非常标准。

Filters are pretty standard in Java web stack.

这篇关于如何使文件下载,而不是在浏览器中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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