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. python面试题之什么是Tkinter ?

    TKinter是一款很知名的Python库,用它我们可以制作图形用户界面.其支持不同的GUI工具和窗口构件,比如按钮.标签.文本框等等.这些工具和构件均有不同的属性,比如维度.颜色.字体等. > ...

  2. poj Drainage Ditches(最大流入门)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 85250   Accepted: 3316 ...

  3. 【知识强化】第四章 网络层 4.8 移动IP

    这节课我们来学习一下移动IP. 那移动IP呢要跟动态IP区分开.动态IP是指,通过使用DHCP协议,在一个局域网内部的一台主机,就可以动态地获得一个IP地址.那这里面的移动IP是什么意思呢? 我们来举 ...

  4. 【记录】解决uni-app 用nginx反向代理出现Invalid Host header问题

    之前解决过一次,后来给忘记了,今天又遇到这个问题,现记录一下 修改uni-app的manifest.json文件  - >源码视图 添加以下代码: "disableHostCheck& ...

  5. SHELL自动化--接口测试

    #!/bin/bash fileNum=`ls /bin/testShell/apiCheck_shell/logs/ | grep $(date "+%Y-%m-%d") | w ...

  6. linux 命令 - ls(列出目录内容)

    ls - 列出目录内容 语法: ls (选项) (参数) 选项: -a:显示所有档案及目录(ls内定将档案名或目录名称为“.”的视为影藏,不会列出): -A:显示除影藏文件“.”和“..”以外的所有文 ...

  7. go语言从例子开始之Example29.关闭通道

    关闭 一个通道意味着不能再向这个通道发送值了.这个特性可以用来给这个通道的接收方传达工作已经完成的信息. Example: package main import "fmt" // ...

  8. Java Exception异常介绍

     一:介绍java异常       异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API中Throwab ...

  9. 2019HDU多校训练第二场 Longest Subarray

    题意:给你一个串,问满足以下条件的子串中最长的是多长:对于每个数字,要么在这个子串没出现过,要么出现次数超过k次. 思路:对于这类问题,常常转化为数据结构的询问问题.我们考虑枚举右端点,对于当前右端点 ...

  10. C++中表示字符串长度

    string的size(), length() 和 char[]的strlen()都是不包括‘\0'的,他们都是“外貌协会”的,只停留在表面.而sizeof则是从内存角度来反映,它是包括’\0‘的.注 ...