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

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

问题描述

我是一个新手,我希望在用户点击下载选项时下载文件,它在浏览器中打开,而不是像另存为/打开这样的下载选项.这里我提到了他们建议使用的每个地方

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 ).我在arraylist中的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 网络堆栈中是非常标准的.

Filters are pretty standard in Java web stack.

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

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