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

servlet文件下载

时间:2016-02-16 13:02:31      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = this.getServletContext().getRealPath("/download/a.jpg");
        String filename = path.substring(path.lastIndexOf("\\") + 1);

        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));

        InputStream in = new FileInputStream(path);
        OutputStream out = response.getOutputStream();

        //注意这里要try..catch
        int len = 0;
        byte buffer[] = new byte[1014];
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
    }

 

servlet文件下载

标签:

原文地址:http://www.cnblogs.com/sysout/p/5192286.html

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