场景描述
使用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包,然后部署到服务器上进行运行