@RequestMapping("/downloadExcel")
@ResponseBody
public void downloadExampleExcel(HttpServletResponse response) {
InputStream inputStream = null;
ServletOutputStream servletOutputStream = null;
try {
Resource resource = new DefaultResourceLoader().getResource("classpath:excel.xls");
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName=" + "excel.xls");
inputStream = resource.getInputStream();
servletOutputStream = response.getOutputStream();
IOUtils.copy(inputStream, servletOutputStream);
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (servletOutputStream != null) {
servletOutputStream.close();
servletOutputStream = null;
}
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
springboot resourse下excel文件的下载
于 2022-03-14 14:56:13 首次发布