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. unity碰撞检测(耗费性能)

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class PengZhua ...

  2. 大哥带我走渗透ii--时间盲注,布尔盲注

    5/27 基于布尔的盲注 我连题目都看不懂555,先去补充一点知识.https://blog.csdn.net/weixin_40709439/article/details/81355856 返回的 ...

  3. Dev常用控件

    GridControl TreeView DEV GridControl小结.. https://blog.csdn.net/happy09li/article/details/7186829 Dev ...

  4. 使用Makefile编译Erlang

    #配置选项,可以是DEBUG和RELEASE CONFIG ?= RELEASE #语言配置,可以是chs(简体中文).cht(繁体中文)等等 Region ?= chs #源文件目录 SOURCE_ ...

  5. 【转】java中JVM的原理

    转载自https://blog.csdn.net/witsmakemen/article/details/28600127/ 一.java虚拟机的生命周期: Java虚拟机的生命周期 一个运行中的Ja ...

  6. 【QT学习】数独游戏

    前几天刷leetcode刷到一题,讲sudokuSolver,写完感觉很有意思,遂想做一个数独游戏,百度了一下如何自动生成题库,参考某位大神安卓下的实现思路,自己做了一套文字版的数独游戏,后来想乘机会 ...

  7. dosbox下载并配置BC3.1及环境变量的方法

    https://www.tuicool.com/articles/v2A3mm--Win8下用DOSBox编写汇编语言 http://www.dosbox.com/ http://www.masm32 ...

  8. iSkysoft iMedia Converter Deluxe for Mac的使用方法

    我们电脑上的播放器大多数的播放格式都比较少,所以在播放其它格式的时候容易出错,不能兼容其它的视频格式.今天小编要给大家推荐一种软件,iSkysoft iMedia Converter Deluxe就是 ...

  9. PHP FILTER_CALLBACK 过滤器

    定义和用法 FILTER_CALLBACK 过滤器调用用户自定义函数来过滤数据. 该过滤器为我们提供了对数据过滤的完全控制. 指定的函数必须存入名为 "options" 的关联数组 ...

  10. 四轴遥控器ADC部分

    一.ADC参考手册学习 A/D转换可以按单次.连续设置采样:可以一一扫描或间断的对多个ADC通道进行采集. ADC的结果有左对齐和右对齐. ADC的输入时钟不得超过14Mhz,它是由PCLK2经分频产 ...