代码改变世界

Spring MVC 文件下载

2015-06-26 23:22  Loull  阅读(347)  评论(0)    收藏  举报

 

        @RequestMapping("download")
        public void download(HttpServletResponse res) throws IOException {
            OutputStream os = res.getOutputStream();
            try {
                res.reset();
                res.setHeader("Content-Disposition", "attachment; filename=dict.txt");
                res.setContentType("application/octet-stream; charset=utf-8");
                os.write(FileUtils.readFileToByteArray(getDictionaryFile()));
                os.flush();
            } finally {
                if (os != null) {
                    os.close();
                }
            }
        }

 

 

 @RequestMapping(value = "/conf/confdwn.json", method = RequestMethod.GET)
    public void downloadConf(HttpServletResponse response, Integer id) throws IOException {
        long start = System.currentTimeMillis();

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=" + conf.getName()
                                                  + ".json");
        response.setContentType("application/octet-stream; charset=utf-8");
        response.getWriter().write(conf.getValue());
        response.flushBuffer();

        long end = System.currentTimeMillis();
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<used time:" + (end - start));
    }

 

http://www.iteye.com/topic/1125784