参考
https://www.jianshu.com/p/b512adecab47
第一种方式在不打包时正常,打包后无法获取
public static void exportTemplate(TemplateEnum type, HttpServletResponse response) {
try {
File file = ResourceUtils.getFile("classpath:templates/" + type.type + "_template.xlsx");
FileInputStream inputStream = new FileInputStream(file);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes(), "ISO8859-1"));
OutputStream outputStream = response.getOutputStream();
int ch;
while ((ch = inputStream.read()) != -1) {
outputStream.write(ch);
}
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
第二种可以再打包后下载资源文件
public static void exportTemplate(TemplateEnum type, HttpServletResponse response) {
ResourceLoader resourceLoader = new DefaultResourceLoader();
String filePath = "classpath:templates/" + type.type + "_template.xlsx";
try {
Resource resource = resourceLoader.getResource(filePath);
@Cleanup InputStream inputStream =resource.getInputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((type.type + "_template.xlsx").getBytes(), "ISO8859-1"));
@Cleanup OutputStream outputStream = response.getOutputStream();
int ch;
while ((ch = inputStream.read()) != -1) {
outputStream.write(ch);
}
} catch (Exception e) {
e.printStackTrace();
}
}