1.HttpClient
- httpclient可以拿到其他项目或者URL下的内容
2.HttpClient用法
a.添加依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
</dependencies>
b.新建一个HttpClientUtils.java类
package com.ym.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/**
* ClassName: HttpClientUtils
* Description: 可以获取指定路径下的网页中的内容
* date: 2020/5/2 14:11
*
* @author YanM
* @since JDK 1.8
*/
public class HttpClientUtils {
/**
* 获取指定baseURL链接所返回的字符串数据
* @param baseURL
* @return
*/
public static String getDataFromURL(String baseURL){
try {
//根据URL创建一个URL对象
URL url=new URL(baseURL);
//通过URL对象的openConnection()方法,强转的到一个httpURLConnection
HttpURLConnection con=(HttpURLConnection) url.openConnection();
//设置请求的超时时间
con.setConnectTimeout(5000);
//设置请求的请求方式
con.setRequestMethod("GET");
//设置请求方式为输入请求
con.setDoInput(true);
//得到请求返回的状态码
int code=con.getResponseCode();
//如果状态码是HTTP_OK(200)
if (code==HttpURLConnection.HTTP_OK){
//通过连接对象的getInputStream()方法的到一个输入流对象
InputStream is=con.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] bytes=new byte[8192];
int len;
while ((len=is.read(bytes))!=-1){
baos.write(bytes,0,len);
}
return new String(baos.toByteArray());
}
} catch (MalformedURLException | ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据指定的baseURL来获取该url对应的字节数组的值
* @param baseURL 要获取数据的url
* @param type 请求类型 GET活着POST
* @return 指定baseURL所对应的字节数组值
* 可以实现图片和视频的下载功能
*/
public static byte[] getByteDataByURL(String baseURL,String type){
try {
URL url=new URL(baseURL);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
if ("GET".equalsIgnoreCase(type)){
con.setRequestMethod("GET");
}else {
con.setRequestMethod("POST");
}
con.setDoInput(true);
int code=con.getResponseCode();
if (code==HttpURLConnection.HTTP_OK){
InputStream is=con.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len;
byte[] data=new byte[8192];
while ((len=is.read(data))!=-1){
baos.write(data,0,len);
}
return baos.toByteArray();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
c.测试功能
import com.ym.utils.HttpClientUtils;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* ClassName: HttpClientUtilsUtilsTest
* Description:
* date: 2020/5/2 14:59
*
* @author YanM
* @since JDK 1.8
*/
public class HttpClientUtilsUtilsTest {
//获取网页中的信息
@Test
public void testGetData(){
String data = HttpClientUtils.getDataFromURL("http://localhost:8080/users/getAll");
System.out.println(data);
}
/**
* 实现下载图片和视频的功能
* http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
* https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4
*/
@Test
public void testGetByteData() throws IOException {
//byte[] datas = HttpClientUtils.getByteDataByURL("http://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&f=JPEG?w=5760&h=3240", "get");
//下载图片
byte[] datas = HttpClientUtils.getByteDataByURL("https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4", "get");
//下载视频
FileOutputStream fos=null;
try {
//fos=new FileOutputStream("gile.jpg");
//下载到本地图片的名字
fos=new FileOutputStream("hello.mp4");
//下载到本地的视频
fos.write(datas);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
if (fos!=null){
fos.close();
fos=null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("download success");
}
}
网友评论