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);
}

猜你喜欢

转载自www.cnblogs.com/duoduo264/p/11766643.html
今日推荐