zoukankan      html  css  js  c++  java
  • 转:Content-disposition中Attachment和inline的区别

    转自:http://itindex.net/detail/52857-content-disposition-attachment?utm_source=tuicool&utm_medium=referral

    java web中下载文件时,我们一般设置 Content-Disposition告诉浏览器下载文件的名称,是否在浏览器中内嵌显示.

    Content-disposition: inline; filename=foobar.pdf

    表示浏览器内嵌显示一个文件

    Content-disposition: attachment; filename=foobar.pdf

    表示会下载文件,如火狐浏览器中 
     
     

    spring mvc中

    @ResponseBody
    	@RequestMapping(value = "/download",produces="application/octet-stream")
    	public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2)
    			throws IOException {
    		byte[]bytes=FileUtils.getBytes4File("D:\Temp\cc.jpg");
    		response.addHeader("Content-Disposition", "inline;filename="a.jpg"");
    		return bytes;
    	}

     如上代码中是内嵌显示图片呢?还是会弹框下载呢?

    答案是:弹框下载

    为什么呢?设置为inline应该是内嵌显示啊!

    因为response content type设置成了"application/octet-stream"

    注意:我们说是内嵌显示还是下载,那 一定是针对可内嵌显示的类型,例如"image/jpeg","image/png"等.

    看下面的例子:设置response content type为" image/jpeg"

    @ResponseBody
    	@RequestMapping(value = "/download",produces="image/jpeg")
    	public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2,String downloadType)
    			throws IOException {
    		byte[]bytes=FileUtils.getBytes4File("D:\Temp\cc.jpg");
    		response.addHeader("Content-Disposition", downloadType+";filename="a.jpg"");
    		return bytes;
    
    	}

     在浏览器中访问:http://localhost:8080/tv_mobile/video/download?downloadType=inline 时就内嵌显示:



     

    当在浏览器中访问:http://localhost:8080/tv_mobile/video/download?downloadType=attachment  时就弹框下载.

    参考:http://hw1287789687.iteye.com/blog/2188480

  • 相关阅读:
    shell-bash学习01基础、打印、环境变量
    css/js(工作中遇到的问题)-2
    git学习 git-flow
    js正则表达式练习
    12 链
    11数据访问
    10访问者,解释器
    08中介者,装饰者
    09 状态,适配器
    调试 scrapy 文件报错:line 48, in _load_handler、line 44, in load_object、 line 37, in import_module
  • 原文地址:https://www.cnblogs.com/libertycode/p/6298292.html
Copyright © 2011-2022 走看看