项长老

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
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);
        }
    }

 

posted on 2016-02-16 11:47  项长老  阅读(217)  评论(0编辑  收藏  举报