下载文件时HttpServletResponse设置响应头的Content-Disposition属性
Content-Disposition属性有两种类型
- inline :将文件内容直接显示在页面
- attachment:弹出对话框让用户下载
弹出对话框下载文件
resp.setHeader("Content-Disposition", "attachment; filename="+fileName);
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>DownloadDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>download</servlet-name>
<servlet-class>com.qf.servlet.DownloadServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
</web-app>
servlet类
public class DownloadServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取文件在tomcat下的路径
String path = getServletContext().getRealPath("download/bb.txt");
//让浏览器收到这份资源的时候, 以下载的方式提醒用户,而不是直接展示
resp.setHeader("Content-Disposition", "attachment; filename=bb.txt");
//转化成输入流
InputStream is = new FileInputStream(path);
OutputStream os = resp.getOutputStream();
int len = 0;
byte[] bts = new byte[1024];
while ((len = is.read(bts)) != -1) {
os.write(bts, 0, len);
}
os.close();
is.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
url:http://localhost:8080/DownloadDemo/download

直接在浏览器页面打开文件
resp.setHeader("Content-Disposition", "inline; filename="+fileName);
修改servlet类
public class DownloadServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取文件在tomcat下的路径
String path = getServletContext().getRealPath("download/bb.txt");
//让浏览器收到这份资源的时候, 以下载的方式提醒用户,而不是直接展示
resp.setHeader("Content-Disposition", "inline; filename=bb.txt");
//转化成输入流
InputStream is = new FileInputStream(path);
OutputStream os = resp.getOutputStream();
int len = 0;
byte[] bts = new byte[1024];
while ((len = is.read(bts)) != -1) {
os.write(bts, 0, len);
}
os.close();
is.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}


下载文件时HttpServletResponse设置响应头的Content-Disposition属性的更多相关文章
- PHP 下载文件时自动添加bom头的方法
首先弄清楚,什么是bom头?在Windows下用记事本之类的程序将文本文件保存为UTF-8格式时,记事本会在文件头前面加上几个不可见的字符(EF BB BF),就是所谓的BOM(Byte order ...
- HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码
HttpServletResponse 和 ServletResponse 都是接口 具体的类型对象是由Servlet容器传递过来 ServletResponse对象的功能分为以下四种: ...
- HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码
原文地址:HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码 HttpServletRespo ...
- 转载: 正确处理浏览器在下载文件时HTTP头的编码问题(Content-Disposition)
最近在做一个下载工具时,发现CSDN上的资源下载时竟然没有被拦截到,经过分析,终于有了一个发现,解决了我之前做文件下载时的乱码问题,所以转载这篇释疑文章,希望有人可以看到,可以从中得到帮助,也用来备忘 ...
- 正确处理下载文件时HTTP头的编码问题(Content-Disposition)
留坑 参考: 正确处理下载文件时HTTP头的编码问题(Content-Disposition) HTTP协议header中Content-Disposition中文文件名乱码 文件下载,content ...
- 下载文件时-修改文件名字 Redis在Windows中安装方法 SVN安装和使用(简单版) WinForm-SQL查询避免UI卡死 Asp.Net MVC Https设置
下载文件时-修改文件名字 1后台代码 /// <summary> /// 文件下载2 /// </summary> /// <param name="Fil ...
- Servlet:浏览器下载文件时文件名为乱码问题
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcep ...
- Firefox下载文件时中文名乱码问题
为了形象化,先看几张不同浏览器下下载文件时的效果图: 1:Firefox 36.0.1 2:IE8 3:Chrome 40.0.2214.93 m 4:360 7.1.1.322 很明显在Firefo ...
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
使用HttpURLConnection下载文件时经常会出现 java.io.FileNotFoundException文件找不到异常,下面介绍下解决办法 首先设置tomcat对get数据的编码:con ...
随机推荐
- python isinstance()函数和type()函数
一.type()用法 描述: python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型.当有三个参数的时候返回一个类对象. 语法: 一个参数:type(object) 三个参数: ...
- 模拟javaWeb责任链的设计
这篇文章介绍了责任链模式的应用:本文介绍如果自己实现一个责任链 定义请求和响应信息 简单定义请求类Request(封装一个字符串) public class Request { String requ ...
- cookie,seesion学习
一,为什么需要cookie和session? 1,Web应用程序是使用HTTP协议传输数据的.然而HTTP协议是无状态的协议.一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的 ...
- 浅析Mysql事务传播行为
传播行为 1.PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就加入该事务,该设置是最常用的设置. 2.PROPAGATION_SUPPORTS:支持当 ...
- 转:动态库路径配置- /etc/ld.so.conf文件
Linux 共享库 Linux 系统上有两类根本不同的 Linux 可执行程序.第一类是静态链接的可执行程序.静态可执行程序包含执行所需的所有函数 — 换句话说,它们是“完整的”.因为这一原因,静态可 ...
- Oracle varchar2或char类型的byte和char的区别
那其中的BYTE和CHAR有什么区别呢 BYTE,用字节指定:VARCHAR2(10 BYTE).这能支持最多10字节的数据,在一个多字节字符集中,这可能只是两个字符.采用多字节字符集时,字节与字符并 ...
- Docker配置阿里云镜像加速pull
前言:默认Docker拉取镜像是从Docker Hub上拉取,但由于防火墙的原因,导致镜像下载非常慢.为了提高拉取镜像的速度,可以配置阿里镜像或是网易镜像加速,通过使用经验推荐配置阿里镜像. 申请个人 ...
- java集合类笔试选择题整理含答案
1.ArrayList list=new ArrayList(20);中的list扩充几次()A. 0B. 1C. 2D. 3答案:A分析:已经指定了长度, 所以不扩容 2.List.Set.Map哪 ...
- HTTP协议-Headers
Request headers 1 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 ...
- Who Saw My Blog
I found that my blog has visitors!!! I wonder who has watched my blog and what did they feel at that ...