SpringBoot导出resource下的文件Demo
@GetMapping("/template")
public void exportFile(HttpServletResponse response){
String filePath = "template" + File.separator + "测试文件.xlsx";
ClassPathResource resource = new ClassPathResource(filePath);
try(
InputStream inputStream= resource.getInputStream();
OutputStream outputStream = response.getOutputStream()
){
response.setContentType("application/vnd.ms-excel");
response.setHeader(
HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=" + URLEncoder.encode("测试文件","utf-8") + ".xlsx");
byte[] buffer = new byte[4096];
int bytesRead;
while((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer,0,bytesRead);
}
outputStream.flush();
} catch ( Exception e){
e.printStackTrace();
}
}