图片批量下载代码
通过读取本地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;
public class Test1 {
public static void httpGetImg(CloseableHttpClient client,String imgUrl,String savePath) {
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; 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"); TimeUnit.SECONDS.sleep(10); } } catch (Exception e) { e.printStackTrace(); }finally{ if(client!=null){ try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
|