Content-disposition中Attachment和inline的区别

来源:互联网 发布:制作书籍的软件 编辑:程序博客网 时间:2024/06/05 12:48

Content-disposition中Attachment和inline的区别

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  时就弹框下载.

0 0
原创粉丝点击