码迷,mamicode.com
首页 > Web开发 > 详细

HttpURLConnection 下载文件

时间:2015-02-10 13:11:43      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

            String fileUrl = request.getRequestURL().substring(0,
                    request.getRequestURL().indexOf("/rest"))
                    + "/test/resources/images/template.jpg";
            URL url = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();
            response.reset();
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Disposition", "attachment; filename="
                    + new String("template.jpg".getBytes("gbk"), "iso-8859-1"));
            OutputStream os = response.getOutputStream();
            InputStream in = conn.getInputStream();
            byte[] b = new byte[1024];
            int r = 0;
            while ((r = in.read(b)) != -1) {
                os.write(b, 0, r);
            }
            os.flush();
            os.close();
            in.close();
            conn.disconnect();

 

HttpURLConnection 下载文件

标签:

原文地址:http://www.cnblogs.com/ahjx1628/p/4283327.html

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