首页 > 其他分享 >Content-Disposition

Content-Disposition

时间:2023-02-23 11:01:45浏览次数:28  
标签:url param fileName Content Disposition response

Content-Disposition

文件下载响应头的设置

content-type 指示响应内容的格式

content-disposition 指示如何处理响应内容。

一般有两种方式:

  • inline:直接在页面显示
  • attchment:以附件形式下载


/**
* 设置强制下载
* @param fileName
* @param response
*/
@RequestMapping("/downOpen")
public void downOpen(@RequestParam("fileName") String fileName,HttpServletResponse response){
String fix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis()+fix);
response.setContentType("application/force-download");
response.setCharacterEncoding("UTF-8");
FtpUtil.downloadOpen(fileName,response);
}

/**
* 设置Content-Disposition :inline 无效直接下载,不打开
* @param fileName
* @param response
*/
@RequestMapping("/forceDownInline")
public void forceDownInline(@RequestParam("fileName") String fileName,HttpServletResponse response){
String fix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
response.setHeader("Content-Disposition", "inline;filename=" + System.currentTimeMillis()+fix);
response.setContentType("application/force-download");
response.setCharacterEncoding("UTF-8");
FtpUtil.downloadOpen(fileName,response);
}

/**
* 不设置 Content-Disposition 在页面显示
* @param url
* @param response
*/
@RequestMapping("/urlOpen/{url}")
public void urlOpen(@PathVariable("url") String url, HttpServletResponse response){
response.setContentType("image/png");
response.setCharacterEncoding("UTF-8");
FtpUtil.urlOpen(url,response);
}
/**
* inline:直接在页面显示
* @param url
* @param response
*/
@RequestMapping("/urlOpenInline/{url}")
public void urlOpenInline(@PathVariable("url") String url, HttpServletResponse response){
response.setHeader("Content-Disposition", "inline;filename=" + System.currentTimeMillis()+".png");
response.setContentType("image/png");
response.setCharacterEncoding("UTF-8");
FtpUtil.urlOpen(url,response);
}

/**
* 附件形式下载,不打开
* @param url
* @param response
*/
@RequestMapping("/urlDownAttachment/{url}")
public void urlDownAttachment(@PathVariable("url") String url, HttpServletResponse response){
response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis()+".png");
response.setContentType("image/png");
response.setCharacterEncoding("UTF-8");
FtpUtil.urlOpen(url,response);
}

/**
* attachment:以附件形式下载,不打开
* @param url
* @param response
*/
@RequestMapping("/downAttachment/{url}")
public void downAttachment(@PathVariable("url") String url, HttpServletResponse response){
response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis()+".png");
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
FtpUtil.urlOpen(url,response);
}

/**
* 设置Content-Disposition :inline 无效 直接下载 不打开
* @param url
* @param response
*/
@RequestMapping("/streamInline/{url}")
public void streamInline(@PathVariable("url") String url, HttpServletResponse response){
response.setHeader("Content-Disposition", "inline;filename=" + System.currentTimeMillis()+".png");
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
FtpUtil.urlOpen(url,response);
}
 

以上这几种设置方式,在<img> 标签中都会展示出图片,不同的是在浏览器访问,

<div style="width: 100px;height: 100px;">
<!--显示有效,预览为二进制流数据-->
<img alt="downOpen" src="http://localhost:8080/ftp/downOpen?fileName=1677072114406.jpeg"/>
</div>
<div style="width: 100px;height: 100px;">
<!--显示有效,预览为图片本图-->
<img alt="urlOpen" src="http://localhost:8080/ftp/urlOpen/1677072114406.jpeg"/>
</div>
<div style="width: 100px;height: 100px;">
<!--显示有效,预览为图片本图-->
<img alt="urlOpenInline" src="http://localhost:8080/ftp/urlOpenInline/1677072114406.jpeg"/>
</div>
<div style="width: 100px;height: 100px;">
<!--显示有效,预览为图片本图-->
<img alt="downAttachment" src="http://localhost:8080/ftp/downAttachment/1677072114406.jpeg"/>
</div>
<div style="width: 100px;height: 100px;">
<!--显示有效,预览为图片本图-->
<img alt="urlDownAttachment" src="http://localhost:8080/ftp/urlDownAttachment/1677072114406.jpeg"/>
</div>
<div style="width: 100px;height: 100px;">
<!--显示有效,预览为图片本图-->
<img alt="streamInline" src="http://localhost:8080/ftp/streamInline/1677072114406.jpeg"/>
</div>
<div style="width: 100px;height: 100px;">
<!--显示有效,预览为二进制流数据-->
<img alt="streamInline" src="http://localhost:8080/ftp/forceDownInline?fileName=1677072114406.jpeg"/>
</div>

在<img>标签中,预览会有不同

设置

response.setContentType("application/octet-stream"); 预览为二进制流数据

其他设置预览为图片

在浏览器访问url时设置

Content-Disposition :attachment 会下载,不打开
Content-Disposition :inline 不下载,直接打开

标签:url,param,fileName,Content,Disposition,response
From: https://www.cnblogs.com/h-c-g/p/17147179.html

相关文章