55 lines
1.6 KiB
Java
55 lines
1.6 KiB
Java
package com.mobai.webMagic.util;
|
|
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.client.HttpClient;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
|
|
/**
|
|
* 该方法使用HttpClient下载图片、PDF、压缩文件等
|
|
* @Author: saisai
|
|
* @Date: 2023/7/25 11:44 下午
|
|
* @Version 1.0
|
|
*/
|
|
public class ImageDownloaderUtil {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ImageDownloaderUtil.class);
|
|
|
|
public static synchronized void downLoadImage(String url, String fileName) {
|
|
//初始化HttpClient
|
|
HttpClient httpClient = HttpClients.custom().build();
|
|
HttpGet httpGet = new HttpGet(url);
|
|
//获取结果
|
|
HttpResponse httpResponse = null;
|
|
try {
|
|
httpResponse = httpClient.execute(httpGet);
|
|
} catch (IOException e) {
|
|
|
|
logger.warn("execute http request fail:", e);
|
|
return;
|
|
}
|
|
//非常简单的下载方法
|
|
try {
|
|
OutputStream out = new FileOutputStream(fileName);
|
|
httpResponse.getEntity().writeTo(out);
|
|
} catch (Exception e) {
|
|
logger.warn("save file fail:", e);
|
|
return;
|
|
}
|
|
try {
|
|
//消耗实体
|
|
EntityUtils.consume(httpResponse.getEntity());
|
|
} catch (IOException e) {
|
|
logger.warn("consume entity fail:", e);
|
|
return;
|
|
}
|
|
}
|
|
}
|