Excel文件使用Java下载 - 错误:文件已损坏
我需要在我的Spring Boot项目中从SRC/Main/Resources/DAO文件夹下载.xls文件。
文件名:daotemplate.xls
我为此写了一个代码。
@GetMapping("/downloadDOA")
public ResponseEntity<Object> downloadTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException{
String filename = "C://Users//Sai//git//r5//src//main//resources//DOA//DOAtemplate.xls";
File file = new File(filename);
InputStreamResource iresource = new InputStreamResource(new FileInputStream(file));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition","attachment;filename=" +file.getName());
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
ResponseEntity<Object> responseEntity = ResponseEntity.ok().headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/vnd.ms-excel")).body(iresource);
return responseEntity;
}
当我使用API时,Excel文件正在下载,但是文件中的数据已损坏。
这是损坏数据的屏幕截图。
任何人都可以帮助我进行什么更改?
I need to download an .xls file from src/main/resources/DAO folder in my spring boot project.
file name : DAOtemplate.xls
I wrote a code for that.
@GetMapping("/downloadDOA")
public ResponseEntity<Object> downloadTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException{
String filename = "C://Users//Sai//git//r5//src//main//resources//DOA//DOAtemplate.xls";
File file = new File(filename);
InputStreamResource iresource = new InputStreamResource(new FileInputStream(file));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition","attachment;filename=" +file.getName());
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
ResponseEntity<Object> responseEntity = ResponseEntity.ok().headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/vnd.ms-excel")).body(iresource);
return responseEntity;
}
When I use the api the excel file is getting downloaded but the data inside the file is getting corrupted.
Here is a screenshot of the corrupted data.
Can anyone pls help me what changes need to be made?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论