java中使用httpClient批量下载图片,代码如下:
DownloadPicTest.java
import java.io.File; import java.io.IOException; import java.io.InputStream; 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 DownloadPicTest { /** * 发送get请求, 下载图片 * * @param url * 路径 * @return */ 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) { CloseableHttpClient client =null; try { client = HttpClients.createDefault(); String url ="https://picsum.photos/300/150/?image="; String path="f:/11/img"; for(int i=0;i<100;i++){ httpGetImg(client,url+i, path+"/"+i+".jpg"); System.out.println("ok"); } } catch (Exception e) { e.printStackTrace(); }finally{ if(client!=null){ try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } } }pom.xml 配置
<!-- apache io操作通用jar包 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- httpclient 支持jar --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.5</version> </dependency>
运行截图:
ok ~~~
Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1