场景描述
使用Springboot实现了一个文件下载的功能,代码如下
String fileName = "template.xls";
res.setHeader("Content-disposition", "attachment;fileName=" + fileName);
res.setContentType("application/vnd.ms-excel;charset=UTF-8");
res.setCharacterEncoding("UTF-8");
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "excelTemplate/template.xls");
FileInputStream input = new FileInputStream(file);
OutputStream out = res.getOutputStream();
byte[] b = new byte[2048];
int len;
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
input.close();
复制代码
其中被下载文件放在resources下,并且在本地使用IDEA启动进行调试测试的时候,能够成功的将文件下载到本地,并且文件正常打开。
报错信息
本地调试完成,将项目打包成jar包,然后部署到服务器上进行运行,这个时候再次访问下载链接,得到一个损坏的,大小为0kb的文件,查看服务器应用日志,获得以下报错信息;
Template.xls cannot be resolved to absolute file path because it does not reside in the file system.
原因
经过资料查询,发现问题出现在ResourceUtils.getFile()这个方法中,来到这个方法的源码处,找到了打印以上报错信息的位置
if (!"file".equals(resourceUrl.getProtocol())) {
throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUrl);
}
复制代码
可以看得出来,如果当前文件的protocol不是file类型,就会出现这个错误。
文件在调试环境中,是以文件的形式存在的,所以可以使用ResourceUtils.getFile()进行访问,而打包以后的jar包形式,文件的形式不复存在,而是只能以流的方式来读取,
解决方法
ClassPathResource classPathResource = new ClassPathResource("excelTemplate/template.xls");
InputStream input = classPathResource.getInputStream();
复制代码
只需要将ResourceUtils.getFile()方法读取,修改为ClassPathResource读取即可。注意这里的Url不需要添加classpath:前缀。
如若有错,烦请指出

关于找一找教程网
本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[Springboot实现文件下载,文件下载后损坏]http://www.zyiz.net/tech/detail-135762.html