zoukankan      html  css  js  c++  java
  • 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.解析

      见源码注释

  • 相关阅读:
    一位年轻女董事长的忠告:不想穷下去就请看
    Ajax拓展02
    Ajax拓展
    Ajax概念及基础
    PHP了解01
    CSS3新增功能03
    CSS3新增功能02
    CSS3新增功能01
    html5介绍
    jQuery学习03
  • 原文地址:https://www.cnblogs.com/sagech/p/5159291.html
Copyright © 2011-2022 走看看