Content-Disposition 响应头,设置文件在浏览器打开还是下载(pdf文件在浏览器预览功能)图片预览功能浏览器下载功能

news/2024/7/24 2:32:51 标签: http

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

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

字段介绍如下:

 
  1. disposition := "Content-Disposition" ":"

  2. disposition-type

  3. *(";" disposition-parm)

  4. disposition-type := "inline"

  5. / "attachment"

  6. / extension-token

  7. ; values are not case-sensitive

  8. disposition-parm := filename-parm / parameter

  9. filename-parm := "filename" "=" value;

Content-Disposition属性有两种类型:inline 和 attachment

inline :将文件内容直接显示在页面

 
  1. File file = new File("rfc1806.txt");

  2. String filename = file.getName();

  3. response.setHeader("Content-Type","text/plain");

  4. response.addHeader("Content-Disposition","inline;filename=" + new String(filename.getBytes(),"utf-8"));

  5. response.addHeader("Content-Length","" + file.length());

https://img-blog.csdnimg.cn/20181113102343465.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpYW94aW9uZ19qaWF4aW4=,size_16,color_FFFFFF,t_70" width="720" />

attachment:弹出对话框让用户下载

 
  1. File file = new File("rfc1806.txt");

  2. String filename = file.getName();

  3. response.setHeader("Content-Type","text/plain");

  4. response.addHeader("Content-Disposition","attachment;filename=" + new String(filename.getBytes(),"utf-8"));

  5. response.addHeader("Content-Length","" + file.length());

https://img-blog.csdnimg.cn/20181113102223369.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpYW94aW9uZ19qaWF4aW4=,size_16,color_FFFFFF,t_70" width="726" />


http://www.niftyadmin.cn/n/957806.html

相关文章

MySQL基础知识-安装MySQL

MySQL基础知识-安装MySQL 前导: 昨天去参加了一个面试,公司不太大,是一家日资企业,在国内有几家分公司,面试官问到了MySQL的基本操作和性能优化,说了一大堆,倒是比较轻松的过了,但…

computed计算属性的用法,模模糊糊;后,用methods也可,为什么

<!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>Vue的方法_侠课岛(9xkd.com)</title> <script src"https://unpkg.com/vuenext"></script> </head> <body><div id"com…

java实现大文件下载(http方式)

java实现大文件下载&#xff0c;基于http方式&#xff0c;控件神马的就不说了。 思路&#xff1a;下载文件无非要读取文件然后写文件&#xff0c;主要这两个步骤&#xff0c;主要难点&#xff1a; 1.读文件&#xff0c;就是硬盘到内存的过程&#xff0c;由于jdk内存限制&#x…

用命令创建MySQL数据库

一、连接MYSQL 格式&#xff1a; mysql -h主机地址 -u用户名 -p用户密码 1、 连接到本机上的MYSQL。 首先打开DOS窗口&#xff0c;然后进入目录mysql\bin&#xff0c;再键入命令mysql -u root -p&#xff0c;回车后提示你输密码.注意用户名前可以有空格也可以没有空格&#xff…

java文件处理(3)——实现文件复制和文件移动

任务要求&#xff1a; 通过二进制流的操作方式把程序调整为可以实现对任何类型文件进行文件复制&#xff08;而不是调用windows命令行的内部命令copy&#xff09;。 通过二进制流的操作方式把程序调整为可以实现对任何类型文件进行文件移动&#xff08;而不是调用windows命令行…

计算属性的setter,getter不懂

计算属性的 Setter 计算属性默认只有 getterhttps://v3.cn.vuejs.org/guide/computed.html#%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E7%BC%93%E5%AD%98-vs-%E6%96%B9%E6%B3%95

Windows10系统下,彻底删除卸载MySQL

本文介绍&#xff0c;在Windows10系统下&#xff0c;如何彻底删除卸载MySQL。。。 1》停止MySQL服务 开始-》所有应用-》Windows管理工具-》服务&#xff0c;将MySQL服务停止。 2》卸载mysql server 控制面板\所有控制面板项\程序和功能&#xff0c;将mysql server卸载掉。 3》…

idea Error running EscpFormApplication. Command line is too long. Shorten the command line via JAR

idea Error running EscpFormApplication. Command line is too long. Shorten the command line via JAR manifest or via a classpath file and rerun. 个人博客里面还有其他idea报错教程&#xff0c;需要的同学可以点击下面链接 Idea运行报错Error running Application: Com…