Content-Disposition属性有两种类型

  1. inline :将文件内容直接显示在页面
  2. attachment:弹出对话框让用户下载

弹出对话框下载文件

resp.setHeader("Content-Disposition", "attachment; filename="+fileName);

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  6. id="WebApp_ID" version="3.0">
  7. <display-name>DownloadDemo</display-name>
  8. <welcome-file-list>
  9. <welcome-file>index.jsp</welcome-file>
  10. </welcome-file-list>
  11.  
  12. <servlet>
  13. <servlet-name>download</servlet-name>
  14. <servlet-class>com.qf.servlet.DownloadServlet2</servlet-class>
  15. </servlet>
  16. <servlet-mapping>
  17. <servlet-name>download</servlet-name>
  18. <url-pattern>/download</url-pattern>
  19. </servlet-mapping>
  20. </web-app>

servlet类

  1. public class DownloadServlet2 extends HttpServlet {
  2.  
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. //获取文件在tomcat下的路径
  6. String path = getServletContext().getRealPath("download/bb.txt");
  7.  
  8. //让浏览器收到这份资源的时候, 以下载的方式提醒用户,而不是直接展示
  9. resp.setHeader("Content-Disposition", "attachment; filename=bb.txt");
  10.  
  11. //转化成输入流
  12. InputStream is = new FileInputStream(path);
  13. OutputStream os = resp.getOutputStream();
  14.  
  15. int len = 0;
  16. byte[] bts = new byte[1024];
  17. while ((len = is.read(bts)) != -1) {
  18. os.write(bts, 0, len);
  19. }
  20. os.close();
  21. is.close();
  22. }
  23.  
  24. @Override
  25. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  26. doGet(req, resp);
  27. }
  28. }

url:http://localhost:8080/DownloadDemo/download

直接在浏览器页面打开文件

resp.setHeader("Content-Disposition", "inline; filename="+fileName);

修改servlet类

  1. public class DownloadServlet2 extends HttpServlet {
  2.  
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. //获取文件在tomcat下的路径
  6. String path = getServletContext().getRealPath("download/bb.txt");
  7.  
  8. //让浏览器收到这份资源的时候, 以下载的方式提醒用户,而不是直接展示
  9. resp.setHeader("Content-Disposition", "inline; filename=bb.txt");
  10.  
  11. //转化成输入流
  12. InputStream is = new FileInputStream(path);
  13. OutputStream os = resp.getOutputStream();
  14.  
  15. int len = 0;
  16. byte[] bts = new byte[1024];
  17. while ((len = is.read(bts)) != -1) {
  18. os.write(bts, 0, len);
  19. }
  20. os.close();
  21. is.close();
  22. }
  23.  
  24. @Override
  25. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  26. doGet(req, resp);
  27. }
  28.  
  29. }

下载文件时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. __str__和__repr__的区别

    有时候我们想让屏幕打印的结果不是对象的内存地址,而是它的值或者其他可以自定义的东西,以便更直观地显示对象内容,可以通过在该对象的类中创建或修改__str__()或__repr__()方法来实现(显示对 ...

  2. Python之数字的格式化输出

    需求: 将数字格式化后输出,并控制数字的位数.对齐.千位分隔符和其他的细节 x = 1234.56789 # Two decimal places of accuracy print(format(x ...

  3. java当中的Timer定时器的4种使用方式

    import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask ...

  4. 使用Surface View来显示图片

    public class YUVImageView extends SurfaceView { private static final String TAG = "YUVImageView ...

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

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

  6. Linux 核心编译与管理

    一般情况下,不需要重新编译核心,除非以下特有的用途 [root@localhost ~]# wget ftp://ftp.twaren.net/pub/Unix/Kernel/linux/kernel ...

  7. jvm加载包名和类名相同的类的规则,以及如何加载包名和类名相同的类(转)

    jvm包括三种类加载器: 第一种:bootstrap classloader:加载java的核心类. 第二种:extension classloader:负责加载jre的扩展目录中的jar包. 第三种 ...

  8. vue 点击当前元素改变样式

    template  <ul>    <li v-for="(relation,index) in relations" v-bind:id="relat ...

  9. ruby之基础语法

    ruby语法之哈希 =>相当于python的字典 ruby语法之数组 =>相当于python的列表 举例: gitaly= Hash.new #建立新Hash类型 gitaly['firs ...

  10. Python星号表达式提取数据

    def drop_first_last(grades): first,*middle,last=grades return middle 这段代码的作用是grades中的元素,第一个和最后一个分别被提 ...