/**
*
* @Title: export @Description: 下载文件 @param fileName @param
* filePath @return @throws IOException ResponseEntity<byte[]> @throws
*/
@RequestMapping("/download")
public ResponseEntity<byte[]> export(HttpServletRequest request, HttpServletResponse response, Integer fileId)
throws IOException {
DocumentFileModel model = documentFileService.getFileById(fileId);
String filePath = model.getUrl();
String fileName = model.getName();
String type = model.getSuffix();
// 文件上传保存路径
String uploadSavePath = "";
// 判断配置文件是否有设置图片保存路径,有则读取配置中的路径
if (GlobalImageUploadConstants.IMAGE_UPLOAD_SAVE_DISK != null) {
uploadSavePath = GlobalImageUploadConstants.IMAGE_UPLOAD_SAVE_DISK;
} else {
uploadSavePath = GlobalHttpServletRequest.getHttpSession().getServletContext().getRealPath("/");
}
filePath = uploadSavePath + filePath;
HttpHeaders headers = new HttpHeaders();
File file = new File(filePath);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// headers.setContentDispositionFormData("attachment", fileName);
/** 文件下载图片乱码处理 **/
fileName = URLEncoder.encode(fileName, "utf-8");
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {// 1.IE浏览器UTF-8
headers.setContentDispositionFormData("attachment", fileName + "." + type);
} else {// 2.其他浏览器attachment;filename*=utf-8'zh_cn
response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + fileName + "." + type);// 重点
}
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
09-05
09-05