Content-Disposition响应头,添加文件在浏览器中打开还是app转载

原创
小哥 2年前 (2022-12-26) 阅读数 42 #大杂烩

Content-Disposition 属性用作下载文件的标识字段,在rfc2616 http://www.rfc-editor.org/rfc/rfc2616.pdf 章节19.5 Additional Features中

有介绍,请看具体介绍 http://www.rfc-editor.org/rfc/rfc1806.txt

字段描述如下:

disposition := "Content-Disposition" ":"
disposition-type
*(";" disposition-parm)
disposition-type := "inline"
/ "attachment"
/ extension-token
; values are not case-sensitive
disposition-parm := filename-parm / parameter
filename-parm := "filename" "=" value;
Content-Disposition有两种类型的属性:inline 和 attachment inline 直接在页面上显示文件内容 attachment:弹出对话框允许用户下载特定示例:

Content-Type: image/jpeg
Content-Disposition: inline;filename=hello.jpg
Content-Description: just a small picture of me
打开页面内的代码:

File file = new File("rfc1806.txt");
String filename = file.getName();
response.setHeader("Content-Type","text/plain");
response.addHeader("Content-Disposition","inline;filename=" + new String(filename.getBytes(),"utf-8"));
response.addHeader("Content-Length","" + file.length());
弹出保存框代码:

File file = new File("rfc1806.txt");
String filename = file.getName();
response.setHeader("Content-Type","text/plain");
response.addHeader("Content-Disposition","attachment;filename=" + new String(filename.getBytes(),"utf-8"));
response.addHeader("Content-Length","" + file.length());

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门