|
@@ -0,0 +1,194 @@
|
|
|
+package api.common.util;
|
|
|
+
|
|
|
+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.client.methods.HttpPost;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONException;
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONObject;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <dependency>
|
|
|
+ * <groupId>org.apache.httpcomponents</groupId>
|
|
|
+ * <artifactId>httpclient</artifactId>
|
|
|
+ * <version>${http.client.version}</version>
|
|
|
+ * </dependency>
|
|
|
+ *
|
|
|
+ * @author martin
|
|
|
+ */
|
|
|
+public class HttpUtil {
|
|
|
+
|
|
|
+// private static final CloseableHttpClient httpClient;
|
|
|
+// private static final RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+// .setSocketTimeout(5000)
|
|
|
+// .setConnectTimeout(5000)
|
|
|
+// .setConnectionRequestTimeout(5000)
|
|
|
+// .setRedirectsEnabled(false)
|
|
|
+// .setExpectContinueEnabled(false)
|
|
|
+// .build();
|
|
|
+//
|
|
|
+// static {
|
|
|
+// PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
|
|
|
+// pool.setMaxTotal(300);
|
|
|
+// pool.setDefaultMaxPerRoute(300);
|
|
|
+// httpClient = HttpClients.custom().setConnectionManager(pool).build();
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * get 请求的参数在 url 里
|
|
|
+ */
|
|
|
+ public static JSONObject get(CloseableHttpClient httpClient, RequestConfig requestConfig, String url, Map<String, String> headers) throws Exception {
|
|
|
+ //1 创建 post 请求
|
|
|
+ HttpGet get = new HttpGet(url);
|
|
|
+ //2 设置请求默认配置
|
|
|
+ get.setConfig(requestConfig);
|
|
|
+ //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
|
|
|
+ if (!CollectionUtil.isEmpty(headers)) {
|
|
|
+ headers.forEach(get::setHeader);
|
|
|
+ }
|
|
|
+ //4 发送请求
|
|
|
+ CloseableHttpResponse response = httpClient.execute(get);
|
|
|
+ //5 处理返回结果,
|
|
|
+ //5-1 如果状态码为200,就是正常返回
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ String result = EntityUtils.toString(response.getEntity());
|
|
|
+ return new JSONObject(result);
|
|
|
+ //如果是下载文件,可以用response.getEntity().getContent()返回InputStream
|
|
|
+ } else {
|
|
|
+ throw new Exception("服务器错误!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过 get 请求下载文件到本地
|
|
|
+ * 发送 get 请求,get 请求的参数在 url 里
|
|
|
+ *
|
|
|
+ * @param url 请求地址
|
|
|
+ * @param headers 请求头,可为 null
|
|
|
+ * @param localFilePath 本地保存的文件路径
|
|
|
+ * @throws IOException 异常
|
|
|
+ * @throws JSONException 异常
|
|
|
+ */
|
|
|
+ public static InputStream get(CloseableHttpClient httpClient, RequestConfig requestConfig,String url, Map<String, String> headers, String localFilePath) throws IOException, JSONException {
|
|
|
+ //1 创建 post 请求
|
|
|
+ HttpGet get = new HttpGet(url);
|
|
|
+ //2 设置请求默认配置
|
|
|
+ get.setConfig(requestConfig);
|
|
|
+ //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
|
|
|
+ if (!CollectionUtil.isEmpty(headers)) {
|
|
|
+ headers.forEach(get::setHeader);
|
|
|
+ }
|
|
|
+ //4 发送请求
|
|
|
+ CloseableHttpResponse response = httpClient.execute(get);
|
|
|
+ //5 处理返回结果,
|
|
|
+ //5-1 如果状态码为200,就是正常返回
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ //如果是下载文件,可以用response.getEntity().getContent()返回InputStream
|
|
|
+ return response.getEntity().getContent();
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("服务器错误!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送 post 请求
|
|
|
+ */
|
|
|
+ public static JSONObject post(CloseableHttpClient httpClient, RequestConfig requestConfig,String url, Map<String, String> headers, Map<String, String> params) throws Exception {
|
|
|
+ //1 创建 post 请求
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
+ //2 设置请求默认配置
|
|
|
+ post.setConfig(requestConfig);
|
|
|
+ //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
|
|
|
+ if (!CollectionUtil.isEmpty(headers)) {
|
|
|
+ headers.forEach(post::setHeader);
|
|
|
+ }
|
|
|
+ //4 设置请求体
|
|
|
+ if (!CollectionUtil.isEmpty(params)) {
|
|
|
+ params.forEach(post::setHeader);
|
|
|
+ }
|
|
|
+ //5 发送请求
|
|
|
+ CloseableHttpResponse response = httpClient.execute(post);
|
|
|
+ //6 处理返回结果,
|
|
|
+ //6-1 如果状态码为200,就是正常返回
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ String result = EntityUtils.toString(response.getEntity());
|
|
|
+ return new JSONObject(result);
|
|
|
+ //如果是下载文件,可以用response.getEntity().getContent()返回InputStream
|
|
|
+ } else {
|
|
|
+ throw new Exception("服务器错误!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
|
|
|
+ * <p>
|
|
|
+ * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?
|
|
|
+ * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
|
|
|
+ * <p>
|
|
|
+ * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130,
|
|
|
+ * 192.168.1.100
|
|
|
+ * <p>
|
|
|
+ * 用户真实IP为: 192.168.1.110
|
|
|
+ */
|
|
|
+ public static String getIpAddress(HttpServletRequest request) {
|
|
|
+ String ip = request.getHeader("x-forwarded-for");
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("HTTP_CLIENT_IP");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getRemoteAddr();
|
|
|
+ }
|
|
|
+ return ip;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void setResponseForError(HttpServletResponse response) {
|
|
|
+ response.setContentType("application/json");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取上传的文件流到 String
|
|
|
+ */
|
|
|
+ public static String getUploadFileString(MultipartFile multipartFile) throws IOException {
|
|
|
+ // 读取流文件
|
|
|
+ InputStream inputStream = multipartFile.getInputStream();
|
|
|
+ // 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
|
|
|
+ InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+
|
|
|
+ // 设置一个接收的String
|
|
|
+ StringBuilder stringBuffer = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ stringBuffer.append(line);
|
|
|
+ }
|
|
|
+ String str = stringBuffer.toString();
|
|
|
+ bufferedReader.close();
|
|
|
+ inputStreamReader.close();
|
|
|
+ inputStream.close();
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|