package com.obcy.util;
import org.apache.http.HttpEntity;
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;
import java.io.*;
public class DownLoad {
//给定资源的绝对地址,和下载到的路径,可以直接下载资源
public static void downLoad(String URL,String path) {
//1.确定url
String targetUrl = URL;
//2.设置请求方式
HttpGet hg = new HttpGet(targetUrl);
hg.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");
//3.创建发送对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//4.发送请求,获取响应对象
//CloseableHttpResponse里面封装了响应头响应体响应行
CloseableHttpResponse response = null;
try {
response = httpClient.execute(hg);
} catch (IOException e) {
e.printStackTrace();
}
//5.解析响应对象
if (response.getStatusLine().getStatusCode() == 200) {
//获取响应体
HttpEntity entity = response.getEntity();
File f = new File(path);
System.out.println("开始下载"+path);
try {
entity.writeTo(new BufferedOutputStream(new FileOutputStream(f)));
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("这个资源不存在");
}
//休眠0.5秒
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(path+"下载完成");
//关闭http
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}