小言_互联网的博客

DownLoadUtils

239人阅读  评论(0)
package com.zcl.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;


public class DownLoadUtils {


    public static void download(String url, String path, String name) throws Exception {
        String[] split1 = url.split("\\?");
        url = split1[split1.length - 1];
        HttpGet httpGET = HttpClientUtils.getHttpGET(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpResponse httpResponse = httpClient.execute(httpGET);
        HttpEntity entity = httpResponse.getEntity();
        String[] split = url.split("/");
        if (StringUtils.isBlank(name)) {
            name = split[split.length - 1];
        }
        if (entity != null) {
            InputStream inputStream = entity.getContent();
            try {
                File file = new File(path, name);
                FileOutputStream fout = new FileOutputStream(file);
                int len = -1;
                byte[] tmp = new byte[1024];
                while ((len = inputStream.read(tmp)) != -1) {
                    fout.write(tmp, 0, len);
                }
                fout.flush();
                fout.close();
            } finally {
                inputStream.close();
            }
        }
        EntityUtils.consume(entity);
        httpClient.close();
    }


}

转载:https://blog.csdn.net/u010663919/article/details/101311789
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场