码迷,mamicode.com
首页 > 其他好文 > 详细

下载存放在项目目录中的文件

时间:2015-09-10 18:59:01      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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

 

下载存放在项目目录中的文件

标签:

原文地址:http://www.cnblogs.com/yuxiaoqi/p/4798705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!