下载excel模板
从resource文件目录下面读取所需要下载的文件模板
@GetMapping("/downloadTemplate") public void downloadTemplate(HttpServletResponse response) { ServletOutputStream servletOutputStream = null; InputStream inputStream = null; try { String filename = "文件名.xlsx"; response.setContentType("application/vnd.ms-excel"); response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); response.addHeader("charset", "utf-8"); response.addHeader("Pragma", "no-cache"); String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()); response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName); inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/文件名.xlsx"); servletOutputStream = response.getOutputStream(); IOUtils.copy(inputStream, servletOutputStream); response.flushBuffer(); } catch (Exception e) { e.printStackTrace(); log.error("下载模板失败"+e.toString()); } finally { try { if (servletOutputStream != null) { servletOutputStream.close(); } if (inputStream != null) { inputStream.close(); } } catch (Exception e) { e.printStackTrace(); log.error("下载模板失败"+e.toString()); } } }