spring mvc 预览pdf 一直会变成下载问题


java web中下载文件时,我们一般设置Content-Disposition告诉浏览器下载文件的名称,是否在浏览器中内嵌显示.

Content-disposition: inline; filename=foobar.pdf

表示浏览器内嵌显示一个文件

 

Content-disposition: attachment; filename=foobar.pdf

 

实例:

 /**
* 合同文件预览
* @param id
* @return
*/
@ApiOperation(value = "合同文件预览", produces = "application/pdf")
@RequestMapping(value = "viewFile",method = RequestMethod.GET,produces="application/pdf")
public ResponseEntity<byte[]> viewFile(@ApiParam(value = "合同id") @RequestParam(value = "id") String id) throws Exception {
ContractFileDTO contractFileDTO = contractFileService.get(id);
InputStream inputStream = null;
//数据库里面存了两种数据,一种是fileId,一种是fileKey,为了兼容
if(contractFileDTO.getFileId().length() > 32){
inputStream = getViewFileInputStreamByKey(contractFileDTO.getFileId());
}else{
inputStream = getViewFileInputStream(contractFileDTO.getFileId());
}

//http头信息
HttpHeaders headers = new HttpHeaders();
//设置编码
String downloadFileName = new String(contractFileDTO.getFileName().getBytes("UTF-8"),"UTF-8");

headers.setContentDispositionFormData("inline;",downloadFileName.trim()+".pdf");


// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

return new ResponseEntity<byte[]>( IOUtils.toByteArray(inputStream),headers,org.springframework.http.HttpStatus.CREATED);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM