我正在尝试使用spring从服务器下载xlsx文件。文件下载但它显示文件已损坏的警报!
这是我的代码:
File file = new File(outputPath+ fileName);
FileInputStream fis = new FileInputStream(file);
response.setContentType("application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
FileCopyUtils.copy(fis, response.getOutputStream());
JSP页面:
$("#download").click(function(){
if($(this).data('clicked', true)){
window.location="http://localhost:8080/IRI-AXCO/downloadFile";
}
}
以下是快照:
感谢帮助!感谢
答案 0 :(得分:0)
我自己找到了解决方案,
@GetMapping("/downloadFile")
public void download(HttpServletRequest request, HttpServletResponse response)throws Exception{
try {
fileName=fileName.replace(" ", "_");
File file = new File(outputPath+ fileName);
FileInputStream fis = new FileInputStream(file);
Path source = Paths.get(outputPath+ fileName);
String contentType=Files.probeContentType(source);
response.setContentType(contentType);
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
FileCopyUtils.copy(fis, response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
上面的代码给了我结果!