我正在返回一个ResponseEntity文件下载。
@RequestMapping("/download")
public ResponseEntity<byte[]> download(){
byte[] fileContent = manager.getFile();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
headers.setPragma("cache");
headers.setExpires(0);
headers.setCacheControl("private");
headers.setContentDispositionFormData("attachment", "sample.pdf");
headers.setContentLength(fileContent.length);
return new ResponseEntity<byte[]>(fileContent, headers, HttpStatus.OK);
}
在chrome I中,请求标题:
Cache-Control:no-store
Cache-Control:no-cache
Cache-Control:private
Content-Disposition:form-data; name="attachment"; filename="sample-pdf.pdf"
Content-Length:1469
Content-Type:application/pdf;charset=UTF-8
Date:Thu, 15 Aug 2013 08:10:25 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:cache
Pragma:no-cache
Server:Apache-Coyote/1.1
如果我使用HttpServletResponse
以旧方式执行此操作public void download(HttpServletResponse response){
byte[] fileContent = manager.getFile();
response.reset();
response.setContentType("application/pdf");
response.setHeader("Pragma", "cache");
response.setHeader("Expires", "0");
response.setHeader("Cache-control", "private");
response.setHeader("Content-Disposition", "attachment; filename=sample.pdf");
FileCopyUtils.copy(fileContent, response.getOutputStream());
}
标题是我想要的
Cache-Control:private
Content-Disposition:form-data; name="attachment"; filename="sample-pdf.pdf"
Content-Length:1469
Content-Type:application/pdf;charset=UTF-8
Date:Thu, 15 Aug 2013 08:10:25 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:cache
Server:Apache-Coyote/1.1
使用ResponseEntity
时,有没有办法清理http标头值?
答案 0 :(得分:0)
不要使用
headers.setContentDispositionFormData("attachment", "sample.pdf");
或其他套装
仅使用添加方法,如:
headers.add("Content-Disposition", "inline;filename=\"sample_"+System.currentTimeMillis()+".pdf\"");