springboot读取文件响应为下载流
/** * zip模板下载 * @param tempCode 模板编码 * @param response */ @RequestMapping("/zip/download") public void zipDownload(HttpServletResponse response) { String zipName = "template.zip"; response.reset(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition","attachment; filename="+zipName); try { OutputStream out = response.getOutputStream();
//ResourceUtils读取模板 File file = ResourceUtils.getFile("classpath:template/template.zip"); InputStream in = new FileInputStream(file); // 循环取出流中的数据 byte[] b = new byte[1024]; int len; while ((len = in.read(b)) !=-1) { out.write(b, 0, len); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }