图片批量下载代码

通过读取本地txt文件中的图片地址,进行批量下载。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.jokerdig.sparseArr;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
* @author Joker大雄
* @data 2022/9/7 - 15:25
**/
public class Test1 {


public static void httpGetImg(CloseableHttpClient client,String imgUrl,String savePath) {

// 发送get请求
HttpGet request = new HttpGet(imgUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(50000).setConnectTimeout(50000).build();

//设置请求头
request.setHeader( "User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1" );

request.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(request);

if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();

InputStream in = entity.getContent();

FileUtils.copyInputStreamToFile(in, new File(savePath));
System.out.println("下载图片成功:"+imgUrl);

}

} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
request.releaseConnection();

}
}
public static void main(String[] args) throws IOException, InterruptedException {

CloseableHttpClient client =null;
// 通过本地txt文件获取下载地址
String str1 = "D:/image.txt";
BufferedReader file1 = new BufferedReader(new FileReader(new File(str1)));
String line; //存放文件中的每一行数据
// 因为我们不知道文件大小,使用集合存放文件
List<String> list1 = new ArrayList<>();
// 循环读取的值不为空
while((line = file1.readLine())!=null){
list1.add(line); // 存放到集合
}
try {
client = HttpClients.createDefault();
// 下载图片存放路径
String path="d:/img";
for(int i=0;i< list1.size();i++){
httpGetImg(client, list1.get(i), path+"/"+i+".png");
System.out.println(path+"/"+i+".png");
System.out.println("ok");
// 暂停10s,下载下一张图片(防止下太快被ban掉)
TimeUnit.SECONDS.sleep(10);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(client!=null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}