实现文件下载、预览
指定返回前端-文件的格式
PDF文件下载:
HttpServletResponse response
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
response.setContentType("application/force-download");
PDF文件预览:
HttpServletResponse response
response.setHeader("Content-Disposition","inline; filename="+ URLEncoder.encode(fileName,"UTF-8"));
response.setContentType("application/pdf");
//不写response.setContentType("application/pdf"); 返回给前端的就是一个文件流,window.open(url)打开的就是乱码,指定contentType之后,URL跳转显示的就是pdf的预览文件
上代码:(前端只需window.open(url)即可)
@CrossOrigin
@ApiOperation(value = API_DESC_PREFIX + "文件下载、预览")
@GetMapping(Constants.REFUND_DOWNLOAD)
public void download(
@ApiParam(value="refundId")@RequestParam("refundId") String refundId,
@ApiParam(required = true,value="类型:1:下载 2:预览")@RequestParam("fileType") String fileType,
HttpServletRequest request, HttpServletResponse response){
HttpHeaders headers = new HttpHeaders();
OutputStream out = null;
String fileName="";
String filePath="";
try {
if(StringUtils.isEmpty(refundId)){
return;
}
if(!StringUtils.isEmpty(refundId)){
RefundApply refundApply = refundApplyService.refundFindById(refundId);
if(refundApply!=null){
filePath=fileServerUrl+refundApply.getFilePath();
fileName=refundApply.getFileName();
}
}
if(StringUtils.isEmpty(filePath)){
return;
}
ResponseEntity<byte[]> entity = restTemplate.exchange(filePath, HttpMethod.GET,new HttpEntity<>(headers), byte[].class);
if (entity.getStatusCodeValue()==200) {
byte[] bytes = entity.getBody();
switch (fileType) {
case "1":
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
response.setContentType("application/force-download");
break;
case "2":
response.setHeader("Content-Disposition","inline; filename="+ URLEncoder.encode(fileName,"UTF-8"));
response.setContentType("application/pdf");
break;
default:
break;
}
out = response.getOutputStream();
out.write(bytes);
out.flush();
}else {
response.setStatus(500);
}
} catch (Exception e) {
log.error(String.format("文件下载报错:%s",e.getMessage()) ,e);
response.setStatus(500);
}finally {
if (out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
返回前端是个流(未指定文件格式)
上代码(前端需要借助组件对流进行分析,例如图片的流借助 img标签的 src请求url即可)
@CrossOrigin
@ApiOperation(value = API_DESC_PREFIX + "图片预览")
@GetMapping(Constants.SPECIAL_WATCH)
public void specialWatch(
@ApiParam(value="path")@RequestParam(value = "path",required = true) String path,
@ApiParam(value="name")@RequestParam(value = "name",required = true) String name,
HttpServletRequest request, HttpServletResponse response){
HttpHeaders headers = new HttpHeaders();
OutputStream out = null;
String filePath="";
try {
if((!StringUtils.isEmpty(path)) && (!StringUtils.isEmpty(name))){
filePath=fileServerUrl+path;
}else {
return;
}
ResponseEntity<byte[]> entity = restTemplate.exchange(filePath, HttpMethod.GET,new HttpEntity<>(headers), byte[].class);
if (entity.getStatusCodeValue()==200) {
byte[] bytes = entity.getBody();
response.setHeader("Content-Disposition","inline; filename="+ URLEncoder.encode(name,"UTF-8"));
out = response.getOutputStream();
out.write(bytes);
out.flush();
}else {
response.setStatus(500);
}
} catch (Exception e) {
log.error(String.format("图片预览报错:%s",e.getMessage()) ,e);
response.setStatus(500);
}finally {
if (out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}