Description
把 Template放在项目中,在 UI提供一个下载链接供 User下载。
Code
1 @Log("INFO") 2 @RequestMapping("/downloadXXXTemplate") 3 public void downloadXXXTemplate(HttpServletRequest req, HttpServletResponse resp) throws Exception { 4 String fileName = "XXXTemplate.xlsx".toString(); 5 InputStream inStream = req.getSession().getServletContext().getResourceAsStream("/WEB-INF/classes/XXXTemplate.xlsx"); 6 // InputStream inStream = req.getServletContext().getResourceAsStream("/WEB-INF/classes/ccar14a/IndicatorTemplate.xlsx"); 7 // ServletRequest.getServletContext() method is introduced in Servlet 3.0, not 2.3. 8 // Need a Servlet 3.0 compatible container such as Tomcat 7, Glassfish 3, etc in Eclipse and set the Target Runtime of your Dynamic Web Project to that container. 9 resp.reset(); 10 resp.setContentType("bin"); 11 resp.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 12 byte[] b = new byte[100]; 13 int len; 14 try { 15 while ((len = inStream.read(b)) > 0) 16 resp.getOutputStream().write(b, 0, len); 17 inStream.close(); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 }
Reference