码迷,mamicode.com
首页 > 编程语言 > 详细

java模拟从http上下载文件

时间:2016-01-26 10:38:56      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:

1.依赖

  Apache httpclient 包。

2.代码

            HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://upload.eloancn.com/downLoadImg.action");
                    StringBody fileName = new StringBody(url);//图片路径
                    MultipartEntity reqEntity = new MultipartEntity();
                    reqEntity.addPart("imgPath", fileName);//fileName文件名称
                    httppost.setEntity(reqEntity);
                    HttpResponse response = httpclient.execute(httppost);
                    int statusCode = response.getStatusLine().getStatusCode();
                    if(statusCode == HttpStatus.SC_OK){
                        System.out.println("服务器正常响应.....下载完成。");
                        HttpEntity resEntity = response.getEntity();
                        String savepath = lei_Fold+"//"+dows[2].substring(dows[2].lastIndexOf("/")+1);//图片保存路径
                        FileOutputStream fos = new FileOutputStream(new File(savepath));
                        resEntity.writeTo(fos);
                    }

3.解析

  见源码注释

java模拟从http上下载文件

标签:

原文地址:http://www.cnblogs.com/sagech/p/5159291.html

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