标签:media entity Servle 二进制流 position pac 模板 cte line
@RequestMapping(value = "dowlondTemplateFile")
public ResponseEntity<byte[]> getFile(HttpServletResponse response) throws IOException {
String fileName = "合同历史模板.xlsx";
ClassPathResource resource = new ClassPathResource("static" + File.separator + "templateFile" + File.separator + fileName);
try {
File file = resource.getFile();
//下载文件路径
if (file.exists()) {
if (SecurityUtil.getOnlineUser() != null) {
LogUtil.info(SecurityUtil.getOnlineUser().getId(), SecurityUtil.getOnlineUser().getName(),
"下载文件", "成功",
"下载文件,id", "");
}
HttpHeaders headers = new HttpHeaders();
//下载显示的文件名,解决中文名称乱码问题
String downloadFileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
//通知浏览器以attachment(下载方式)打开图片
headers.setContentDispositionFormData("attachment", downloadFileName);
//application/octet-stream : 二进制流数据(最常见的文件下载)。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
} else {
throw new FileNotExistException();
}
} catch (IOException e) {
return null;
}
}
解决 pom中加入如下 这个插件可以避免xlsx文件在resource目录下被自动压缩,这样就可以正常下载,打开了
<plugin> <groupId>org.apache.maven.plugins</groupId> <version>2.6</version> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> <nonFilteredFileExtensions> <nonFilteredFileExtension>xlsx</nonFilteredFileExtension> </nonFilteredFileExtensions> </configuration> </plugin>
标签:media entity Servle 二进制流 position pac 模板 cte line
原文地址:https://www.cnblogs.com/Ai-Hen-Jiao-zhi/p/13921428.html