Skip to content

Commit 415e286

Browse files
committed
TestWeb添加AddrUtil,OkHttpUtil
1 parent a0def42 commit 415e286

File tree

5 files changed

+237
-76
lines changed

5 files changed

+237
-76
lines changed

TestWeb/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@
123123
<version>2.1.6</version>
124124
</dependency>
125125

126+
<!-- hutool -->
127+
<dependency>
128+
<groupId>cn.hutool</groupId>
129+
<artifactId>hutool-all</artifactId>
130+
<version>5.7.2</version>
131+
</dependency>
132+
133+
<!-- okhttp -->
134+
<dependency>
135+
<groupId>io.github.openfeign</groupId>
136+
<artifactId>feign-okhttp</artifactId>
137+
<version>10.2.3</version>
138+
</dependency>
139+
126140
</dependencies>
127141

128142
<build>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.wang.util;
2+
3+
import cn.hutool.core.exceptions.ExceptionUtil;
4+
import okhttp3.MediaType;
5+
import okhttp3.OkHttpClient;
6+
import okhttp3.Request;
7+
import okhttp3.RequestBody;
8+
import okhttp3.Response;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.io.IOException;
13+
14+
/**
15+
* OkHttpUtil
16+
*
17+
* @author wliduo[i@dolyw.com]
18+
* @date 2021/8/23 10:08
19+
*/
20+
public class OkHttpUtil {
21+
22+
/**
23+
* logger
24+
*/
25+
private static final Logger logger = LoggerFactory.getLogger(OkHttpUtil.class);
26+
27+
/**
28+
* MEDIA_TYPE_JSON
29+
*/
30+
private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
31+
32+
/**
33+
* MEDIA_TYPE_TEXT
34+
*/
35+
private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/html;charset=utf-8");
36+
37+
/**
38+
* Get请求
39+
*
40+
* @param url
41+
* @return java.lang.String
42+
* @throws
43+
* @author wliduo[i@dolyw.com]
44+
* @date 2021/8/23 10:18
45+
*/
46+
public static String get(String url) {
47+
String result = null;
48+
OkHttpClient client = new OkHttpClient.Builder().build();
49+
Request request = new Request.Builder().url(url).build();
50+
logger.info("请求地址:{}", url);
51+
try (Response response = client.newCall(request).execute()) {
52+
result = response.body().string();
53+
logger.info("请求地址:{},请求结果:{}", url, result);
54+
} catch (Exception e) {
55+
logger.error("请求地址:{},请求异常:{}", url, ExceptionUtil.stacktraceToOneLineString(e));
56+
}
57+
return result;
58+
}
59+
60+
/**
61+
* postText请求
62+
*
63+
* @param url
64+
* @param data 提交的参数为key=value&key1=value1的形式
65+
* @return java.lang.String
66+
* @throws
67+
* @author wliduo[i@dolyw.com]
68+
* @date 2021/8/23 10:18
69+
*/
70+
public static String postText(String url, String data) {
71+
String result = null;
72+
OkHttpClient httpClient = new OkHttpClient.Builder().build();
73+
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_TEXT, data);
74+
Request request = new Request.Builder().url(url).post(requestBody).build();
75+
logger.info("请求地址:{},请求参数:{}", url, data);
76+
try (Response response = httpClient.newCall(request).execute()) {
77+
result = response.body().string();
78+
logger.info("请求地址:{},请求参数:{},请求结果:{}", url, data, result);
79+
} catch (IOException e) {
80+
logger.error("请求地址:{},请求参数:{},请求异常:{}", url, data, ExceptionUtil.stacktraceToOneLineString(e));
81+
}
82+
return result;
83+
}
84+
85+
/**
86+
* postJson请求
87+
*
88+
* @param url
89+
* @param json json串
90+
* @return java.lang.String
91+
* @throws
92+
* @author wliduo[i@dolyw.com]
93+
* @date 2021/8/23 10:19
94+
*/
95+
public static String postJson(String url, String json) {
96+
String result = null;
97+
OkHttpClient httpClient = new OkHttpClient.Builder().build();
98+
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json);
99+
Request request = new Request.Builder().url(url).post(requestBody).build();
100+
logger.info("请求地址:{},请求参数:{}", url, json);
101+
try {
102+
Response response = httpClient.newCall(request).execute();
103+
result = response.body().string();
104+
logger.info("请求地址:{},请求参数:{},请求结果:{}", url, json, result);
105+
} catch (IOException e) {
106+
logger.error("请求地址:{},请求参数:{},请求异常:{}", url, json, ExceptionUtil.stacktraceToOneLineString(e));
107+
}
108+
return result;
109+
}
110+
111+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.wang.util.common;
2+
3+
import cn.hutool.core.util.StrUtil;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import java.net.InetAddress;
9+
import java.net.UnknownHostException;
10+
11+
/**
12+
* 获取IP地址工具
13+
*
14+
* @author wliduo[i@dolyw.com]
15+
* @date 2021/9/9 19:19
16+
*/
17+
public class AddrUtil {
18+
19+
/**
20+
* logger
21+
*/
22+
private static final Logger logger = LoggerFactory.getLogger(AddrUtil.class);
23+
24+
/**
25+
* unknown
26+
*/
27+
private final static String UNKNOWN_STR = "unknown";
28+
29+
/**
30+
* 127.0.0.1
31+
*/
32+
private final static String LOCALHOST_STR = "127.0.0.1";
33+
34+
/**
35+
* 0:0:0:0:0:0:0:1
36+
*/
37+
private final static String LOCALHOST_IP_STR = "0:0:0:0:0:0:0:1";
38+
39+
/**
40+
* 获取客户端IP地址
41+
*/
42+
public static String getRemoteAddr(HttpServletRequest request) {
43+
String ip = request.getHeader("X-Forwarded-For");
44+
if (isEmptyIp(ip)) {
45+
ip = request.getHeader("Proxy-Client-IP");
46+
if (isEmptyIp(ip)) {
47+
ip = request.getHeader("WL-Proxy-Client-IP");
48+
if (isEmptyIp(ip)) {
49+
ip = request.getHeader("HTTP_CLIENT_IP");
50+
if (isEmptyIp(ip)) {
51+
ip = request.getHeader("X-Real-IP");
52+
if (isEmptyIp(ip)) {
53+
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
54+
if (isEmptyIp(ip)) {
55+
ip = request.getRemoteAddr();
56+
if (LOCALHOST_STR.equals(ip) || LOCALHOST_IP_STR.equals(ip)) {
57+
// 根据网卡取本机配置的IP
58+
ip = getLocalAddr();
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
} else if (ip.length() > 15) {
66+
String[] ips = ip.split(",");
67+
for (int index = 0; index < ips.length; index++) {
68+
String strIp = ips[index];
69+
if (!isEmptyIp(ip)) {
70+
ip = strIp;
71+
break;
72+
}
73+
}
74+
}
75+
return ip;
76+
}
77+
78+
/**
79+
* IP是否为空
80+
*
81+
* @param ip
82+
* @return
83+
*/
84+
private static boolean isEmptyIp(String ip) {
85+
if (StrUtil.isEmpty(ip) || UNKNOWN_STR.equalsIgnoreCase(ip)) {
86+
return true;
87+
}
88+
return false;
89+
}
90+
91+
/**
92+
* 获取本机的IP地址
93+
*/
94+
public static String getLocalAddr() {
95+
try {
96+
return InetAddress.getLocalHost().getHostAddress();
97+
} catch (UnknownHostException e) {
98+
logger.error("InetAddress.getLocalHost()-error", e);
99+
}
100+
return "";
101+
}
102+
}

TestWeb/src/main/java/com/wang/util/common/NetworkUtil.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

TestWeb/src/test/java/com/wang/other/TestOther.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ public void t10() throws Exception {
315315
String s3 = URLDecoder.decode(s2, "UTF-8");
316316
System.out.println(s2);
317317
System.out.println(s3);
318+
319+
Integer i1 = 50000;
320+
Integer i2 = 50000;
321+
System.out.println(i1.equals(i2));
322+
323+
System.out.println("/PayController/elifePayback".contains("PayController/elifePayback"));
324+
int i = 0;
325+
System.out.println(String.format("P%d*S%d * (1-0) * (S%d/Q%d)", i+2, i+2, i+2, i+2));
318326
}
319327

320328
@Test
@@ -348,6 +356,8 @@ public void t12() {
348356
public void testMoney() {
349357
BigDecimal orderAmt = new BigDecimal("0005.5").divide(new BigDecimal("100"));
350358
System.out.println(orderAmt.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
359+
360+
System.out.println(new BigDecimal("A0"));
351361
}
352362

353363
public static void main(String[] args) throws Exception {

0 commit comments

Comments
 (0)