package com.ruoyi.web.controller.tool;
|
|
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
import org.apache.http.util.EntityUtils;
|
|
import java.io.ByteArrayOutputStream;
|
import java.io.InputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.Map;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* @author zhy
|
* @title: HttpClientUtil
|
* @projectName car_park
|
* @description: http连接工具类
|
* @date 2019/10/2219:23
|
*/
|
public class HttpClientUtil {
|
private static PoolingHttpClientConnectionManager connectionManager;
|
|
|
{
|
//1.创建连接池管理器
|
connectionManager = new PoolingHttpClientConnectionManager(60000,
|
TimeUnit.MILLISECONDS);
|
connectionManager.setMaxTotal(1000);
|
connectionManager.setDefaultMaxPerRoute(50);
|
}
|
|
public static HttpResult pushHttpRequsetXml(String url, String xml, Map<String, String> header) throws Exception{
|
HttpPost httpPost = new HttpPost(url);
|
httpPost.setConfig(getRequestConfig());
|
for(String key : header.keySet()){
|
httpPost.setHeader(key, header.get(key));
|
}
|
httpPost.setHeader("Content-Type", "application/xml");
|
httpPost.setEntity(new StringEntity(xml, "UTF-8"));
|
CloseableHttpResponse httpResponse = getHttpCline().execute(httpPost);
|
int statusCode = httpResponse.getStatusLine().getStatusCode();
|
String content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
|
HttpResult httpResult = HttpResult.getHttpResult(statusCode, content);
|
close(httpResponse);
|
return httpResult;
|
}
|
private static void close(CloseableHttpResponse httpResponse){
|
try {
|
if(null != httpResponse){
|
EntityUtils.consume(httpResponse.getEntity());//此处高能,通过源码分析,由EntityUtils是否回收HttpEntity
|
httpResponse.close();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally {
|
try {
|
if(null != httpResponse){
|
httpResponse.close();
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
}
|
private static CloseableHttpClient getHttpCline(){
|
return HttpClients.custom()
|
.setConnectionManager(connectionManager)
|
.disableAutomaticRetries()
|
.build();
|
}
|
private static RequestConfig getRequestConfig(){
|
RequestConfig.Builder builder = RequestConfig.custom();
|
builder.setSocketTimeout(60000)//3.1设置客户端等待服务端返回数据的超时时间
|
.setConnectTimeout(30000)//3.2设置客户端发起TCP连接请求的超时时间
|
.setExpectContinueEnabled(true)
|
.setConnectionRequestTimeout(30000);//3.3设置客户端从连接池获取链接的超时时间
|
return builder.build();
|
}
|
|
/**
|
* @param strUrl
|
* @return byte[]
|
* @throws
|
* @description: 获取网络图片转成字节流
|
* @author zhy
|
* @date 2019/10/23 8:59
|
*/
|
public static byte[] getImageFromNetByUrl(String strUrl) {
|
if (!isURL(strUrl)){
|
return null;
|
}
|
try {
|
URL url = new URL(strUrl);
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
conn.setRequestMethod("GET");
|
conn.setConnectTimeout(2 * 1000);
|
InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据
|
byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据
|
return btImg;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 从输入流中获取字节流数据
|
*
|
* @param inStream 输入流
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] readInputStream(InputStream inStream) throws Exception {
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
byte[] buffer = new byte[10240];
|
int len = 0;
|
while ((len = inStream.read(buffer)) != -1) {
|
outStream.write(buffer, 0, len);
|
}
|
inStream.close();
|
return outStream.toByteArray();
|
}
|
|
public static boolean isURL(String str) {
|
str = str.toLowerCase();
|
String regex = "^((https|http|ftp|rtsp|mms)?://)"
|
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"
|
+ "(([0-9]{1,3}\\.){3}[0-9]{1,3}"
|
+ "|"
|
+ "([0-9a-z_!~*'()-]+\\.)*"
|
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\."
|
+ "[a-z]{2,6})"
|
+ "(:[0-9]{1,5})?"
|
+ "((/?)|"
|
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
|
return str.matches(regex);
|
}
|
|
|
}
|
|