Content-Disposition属性有两种类型

  1. inline :将文件内容直接显示在页面
  2. 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属性的更多相关文章

  1. PHP 下载文件时自动添加bom头的方法

    首先弄清楚,什么是bom头?在Windows下用记事本之类的程序将文本文件保存为UTF-8格式时,记事本会在文件头前面加上几个不可见的字符(EF BB BF),就是所谓的BOM(Byte order ...

  2. HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码

    HttpServletResponse  和 ServletResponse  都是接口 具体的类型对象是由Servlet容器传递过来   ServletResponse对象的功能分为以下四种:   ...

  3. HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码

    原文地址:HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码 HttpServletRespo ...

  4. 转载: 正确处理浏览器在下载文件时HTTP头的编码问题(Content-Disposition)

    最近在做一个下载工具时,发现CSDN上的资源下载时竟然没有被拦截到,经过分析,终于有了一个发现,解决了我之前做文件下载时的乱码问题,所以转载这篇释疑文章,希望有人可以看到,可以从中得到帮助,也用来备忘 ...

  5. 正确处理下载文件时HTTP头的编码问题(Content-Disposition)

    留坑 参考: 正确处理下载文件时HTTP头的编码问题(Content-Disposition) HTTP协议header中Content-Disposition中文文件名乱码 文件下载,content ...

  6. 下载文件时-修改文件名字 Redis在Windows中安装方法 SVN安装和使用(简单版) WinForm-SQL查询避免UI卡死 Asp.Net MVC Https设置

    下载文件时-修改文件名字   1后台代码 /// <summary> /// 文件下载2 /// </summary> /// <param name="Fil ...

  7. Servlet:浏览器下载文件时文件名为乱码问题

    1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcep ...

  8. Firefox下载文件时中文名乱码问题

    为了形象化,先看几张不同浏览器下下载文件时的效果图: 1:Firefox 36.0.1 2:IE8 3:Chrome 40.0.2214.93 m 4:360 7.1.1.322 很明显在Firefo ...

  9. 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法

    使用HttpURLConnection下载文件时经常会出现 java.io.FileNotFoundException文件找不到异常,下面介绍下解决办法 首先设置tomcat对get数据的编码:con ...

随机推荐

  1. Codeforces 1178F DP

    题意:有一张白纸条,你需要给这张纸条染色.染色从颜色1开始染色,每次选择纸条的一段染色时,这一段的颜色必须是相同的.现在给你染色后的纸条,问有多少种染色方案? F1: 思路:最开始的想法是以染色顺序为 ...

  2. JavaScript 下载大文件解决方案(Blob+OjbectURL)

    结合Blob和OjbectURL实现更大的文件下载: var a = document.createElement('a'); var txt = '.....content....'; for(va ...

  3. 利用Stream模式进行文件拷贝

    const fs = require('fs'); const file = fs.createReadStream("readfile.js"); const outputFil ...

  4. shell巡检草拟

    #!/bin/bash phy_cpu=$(cat /proc/cpuinfo | grep "physical id"|sort | uniq | wc -l) logic_cp ...

  5. Concurrent - 并发框架

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11426833.html SynchronizedMap和ConcurrentHashMap有什么区别? ...

  6. leetcode-165周赛-1277-统计全为1的正方形子矩阵

    题目描述: 自己的提交: class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matri ...

  7. Mac上的Apache 开启,停止,重启

    sudo apachectl -k start     启动 sudo apachectl -k stop     停止   sudo apachectl -k restart   重启

  8. leetcode骚题目列表

    114,二叉树原地前序遍历转链表 令人不舒服的空间限制 4,O(logn)寻找两个数组的中位数 感觉诡异又很其妙的二分 279,判断一个数可拆成最少几个平方数的和 有O(n)解法,如果把sqrt视为O ...

  9. 【Flutter学习】基本组件之上下刷新列表(一)

    一,概述 RefreshIndicator是Flutter基于Material设计语言内置的控件,集合了下拉手势.加载指示器和刷新操作一体,可玩性比FutureBuilder差了一大截,不过大家也用过 ...

  10. ubuntu下安装apidoc

    1.到http://nodejs.cn/download/下载nodejs 可以复制链接 使用wget下载更加快速 选择对应的操作系统位数 下载到/usr/local/src 此处强烈不建议编译安装 ...