记录一下工具类
package com.test.util;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
public class HttpUtils {
private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
private static CloseableHttpClient httpClient = null;
public HttpUtils() {
}
public static String getHostUrl(HttpServletRequest request) {
String hostUrl = request.getScheme() + "://" + request.getServerName();
if (!"80".equals(request.getServerPort())) {
hostUrl = hostUrl + ":" + request.getServerPort();
}
return hostUrl;
}
public static String doGet(String url) {
return doGet(url, (Map)null);
}
public static String doGet(String url, Map<String, String> params) {
String resultString = "";
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
if (params != null) {
Iterator var5 = params.keySet().iterator();
while(var5.hasNext()) {
String key = (String)var5.next();
builder.addParameter(key, (String)params.get(key));
}
}
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (null != entity) {
resultString = EntityUtils.toString(entity, StandardCharsets.UTF_8);
}
} catch (Exception var16) {
var16.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException var15) {
var15.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, (Map)null, (Map)null);
}
public static String doPost(String url, Map<String, String> param) {
return doPost(url, param, (Map)null);
}
public static String doPost(String url, Map<String, String> params, Map<String, String> headers) {
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
if (null != headers && headers.size() > 0) {
Iterator var6 = headers.keySet().iterator();
while(var6.hasNext()) {
String key = (String)var6.next();
httpPost.setHeader(key, (String)headers.get(key));
}
}
if (null != params && params.size() > 0) {
List<NameValuePair> paramList = new ArrayList();
Iterator var21 = params.keySet().iterator();
while(var21.hasNext()) {
String key = (String)var21.next();
paramList.add(new BasicNameValuePair(key, (String)params.get(key)));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, StandardCharsets.UTF_8);
httpPost.setEntity(entity);
}
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (null != entity) {
resultString = EntityUtils.toString(entity, StandardCharsets.UTF_8);
}
} catch (Exception var17) {
logger.error(var17.getMessage());
} finally {
try {
response.close();
} catch (IOException var16) {
logger.error(var16.getMessage());
}
}
return resultString;
}
public static String doPostJson(String url, String json) {
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
} catch (Exception var14) {
var14.printStackTrace();
} finally {
try {
response.close();
} catch (IOException var13) {
var13.printStackTrace();
}
}
return resultString;
}
public static String doPostFile(String url, MultipartFile mulFile, Map<String, String> param) throws Exception {
File file = FileUtils.multipartFileToFile(mulFile);
return doPostFile(url, file, param);
}
public static String doPostFile(String url, File file, Map<String, String> param) throws Exception {
String result = "";
try {
HttpPost post = new HttpPost(url);
RequestConfig config = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(20000).build();
post.setConfig(config);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ContentType contentType = ContentType.create("application/octet-stream", StandardCharsets.UTF_8);
Iterator var8 = param.keySet().iterator();
while(var8.hasNext()) {
String key = (String)var8.next();
String value = (String)param.get(key);
if (StringUtils.isNotEmpty(value)) {
builder.addPart(key, new StringBody(value, contentType));
}
}
builder.addBinaryBody("file", file);
HttpEntity entity = builder.build();
post.setEntity(entity);
CloseableHttpResponse e = httpClient.execute(post);
HttpEntity resEntity = e.getEntity();
if (entity != null) {
result = EntityUtils.toString(resEntity, StandardCharsets.UTF_8);
}
File del = new File(file.toURI());
if (del.exists()) {
del.delete();
}
} catch (IOException var12) {
var12.printStackTrace();
}
return result;
}
public static boolean isAjaxRequest(HttpServletRequest webRequest) {
String requestedWith = webRequest.getHeader("X-Requested-With");
return requestedWith != null ? "XMLHttpRequest".equals(requestedWith) : false;
}
public static boolean isAjaxUploadRequest(HttpServletRequest webRequest) {
return webRequest.getParameter("ajaxUpload") != null;
}
public static boolean isGetMethod(HttpServletRequest request) {
String method = request.getMethod();
return "GET".equalsIgnoreCase(method);
}
static {
PoolingHttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager();
clientConnectionManager.setValidateAfterInactivity(2000);
clientConnectionManager.setMaxTotal(500);
clientConnectionManager.setDefaultMaxPerRoute(50);
httpClient = HttpClients.createMinimal(clientConnectionManager);
}
}