java模拟从http上下载文件

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.解析

  见源码注释

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