httpclient 下载文件

这里写自定义目录标题

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.go</groupId>
    <artifactId>tool-com</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <!-- <scope>test</scope> -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>

    </dependencies>

</project>

download

package cn.go;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class HttpClientTest {
    public static void main(String[] args) {
        try {
            
            download("6010102080720200036802");
        } catch (IOException e) {
            e.printStackTrace();
        }
        /* try {
            File file = new File("d:/policyNo_list.txt");
            BufferedReader  reader = new BufferedReader(new FileReader(file));
            String policyNo;
            while ((policyNo = reader.readLine()) != null) {
                System.out.println(policyNo);
                download(policyNo);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }

    private static void download(String str) throws IOException {

        String urlPrefix = "http://xxx";

        String url = urlPrefix + "/xxx?policyNo=" + str;
        String result= null;
        String defaultCharset = "utf-8";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpGet);
        if (response != null) {
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                result = EntityUtils.toString(resEntity, defaultCharset);
            }
        }
        System.out.println(result);
        // http://ip:port
        
        //planCode = code + "car_"+ planCode + ".ftl";
        String fileUrl = urlPrefix + "/xxxx"+result;
        System.out.println(fileUrl);
        HttpGet httpGetFile = new HttpGet(fileUrl);
        HttpResponse fileResponse = httpclient.execute(httpGetFile);
        if (fileResponse != null) {
            HttpEntity resEntity = fileResponse.getEntity();
            Header contentType = fileResponse.getEntity().getContentType();
            System.out.println(contentType);
            if (resEntity != null) {
                result = EntityUtils.toString(resEntity, defaultCharset);
                System.out.println(result);
                if(!"xxxx".equals(result)){
                    System.exit(1);
                }
                return;
            }
        }

        InputStream inputStream = fileResponse.getEntity().getContent();
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,这里配置的大一些
        int len = 0;
        while((len = inputStream.read(buff)) > 0) {
            //把从服务端读取的文件流保存到ByteArrayOutputSteam中
            byteArray.write(buff, 0, len);
            byteArray.flush();
        }
        inputStream.close();
        ((CloseableHttpResponse) fileResponse).close();
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值