Skip to content

Commit 7b2547d

Browse files
committed
✨ 扩展 Map 工具类,String 转 Map
1 parent 2087069 commit 7b2547d

File tree

3 files changed

+123
-5
lines changed

3 files changed

+123
-5
lines changed

src/main/java/com/xkcoding/http/util/MapUtil.java

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
package com.xkcoding.http.util;
1818

19+
import com.xkcoding.http.constants.Constants;
1920
import lombok.experimental.UtilityClass;
2021

21-
import java.util.ArrayList;
22-
import java.util.ConcurrentModificationException;
23-
import java.util.List;
24-
import java.util.Map;
22+
import java.util.*;
2523
import java.util.function.BiConsumer;
2624

2725
/**
@@ -98,4 +96,103 @@ public String parseMapToString(Map<String, String> params, boolean encode) {
9896
});
9997
return String.join("&", paramList);
10098
}
99+
100+
/**
101+
* 字符串转map,字符串格式为 {@code xxx=xxx&xxx=xxx}
102+
*
103+
* @param str 待转换的字符串
104+
* @param decode 是否解码
105+
* @return map
106+
*/
107+
public Map<String, String> parseStringToMap(String str, boolean decode) {
108+
str = preProcess(str);
109+
110+
Map<String, String> params = new HashMap<>(16);
111+
if (StringUtil.isEmpty(str)) {
112+
return params;
113+
}
114+
115+
if (!str.contains("&")) {
116+
params.put(decode(str, decode), Constants.EMPTY);
117+
return params;
118+
}
119+
120+
final int len = str.length();
121+
String name = null;
122+
// 未处理字符开始位置
123+
int pos = 0;
124+
// 未处理字符结束位置
125+
int i;
126+
// 当前字符
127+
char c;
128+
for (i = 0; i < len; i++) {
129+
c = str.charAt(i);
130+
// 键值对的分界点
131+
if (c == '=') {
132+
if (null == name) {
133+
// name可以是""
134+
name = str.substring(pos, i);
135+
}
136+
pos = i + 1;
137+
}
138+
// 参数对的分界点
139+
else if (c == '&') {
140+
if (null == name && pos != i) {
141+
// 对于像&a&这类无参数值的字符串,我们将name为a的值设为""
142+
addParam(params, str.substring(pos, i), Constants.EMPTY, decode);
143+
} else if (name != null) {
144+
addParam(params, name, str.substring(pos, i), decode);
145+
name = null;
146+
}
147+
pos = i + 1;
148+
}
149+
}
150+
151+
// 处理结尾
152+
if (pos != i) {
153+
if (name == null) {
154+
addParam(params, str.substring(pos, i), Constants.EMPTY, decode);
155+
} else {
156+
addParam(params, name, str.substring(pos, i), decode);
157+
}
158+
} else if (name != null) {
159+
addParam(params, name, Constants.EMPTY, decode);
160+
}
161+
162+
return params;
163+
}
164+
165+
private void addParam(Map<String, String> params, String key, String value, boolean decode) {
166+
key = decode(key, decode);
167+
value = decode(value, decode);
168+
if (params.containsKey(key)) {
169+
params.put(key, params.get(key) + "," + value);
170+
} else {
171+
params.put(key, value);
172+
}
173+
}
174+
175+
private String decode(String str, boolean decode) {
176+
return decode ? UrlUtil.urlDecode(str) : str;
177+
}
178+
179+
180+
private String preProcess(String str) {
181+
if (StringUtil.isEmpty(str)) {
182+
return str;
183+
}
184+
// 去除 URL 路径信息
185+
int beginPos = str.indexOf("?");
186+
if (beginPos > -1) {
187+
str = str.substring(beginPos + 1);
188+
}
189+
190+
// 去除 # 后面的内容
191+
int endPos = str.indexOf("#");
192+
if (endPos > -1) {
193+
str = str.substring(0, endPos);
194+
}
195+
196+
return str;
197+
}
101198
}

src/main/java/com/xkcoding/http/util/StringUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class StringUtil {
3131

3232
public static boolean isEmpty(String str) {
33-
return null == str || str.isEmpty();
33+
return null == str || str.trim().isEmpty();
3434
}
3535

3636
public static boolean isNotEmpty(String str) {

src/main/java/com/xkcoding/http/util/UrlUtil.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.xkcoding.http.util;
1818

19+
import cn.hutool.core.exceptions.UtilException;
1920
import com.xkcoding.http.constants.Constants;
2021
import lombok.experimental.UtilityClass;
2122

2223
import java.io.UnsupportedEncodingException;
24+
import java.net.URLDecoder;
2325
import java.net.URLEncoder;
2426

2527
/**
@@ -49,4 +51,23 @@ public String urlEncode(String value) {
4951
throw new RuntimeException("Failed To Encode Uri", e);
5052
}
5153
}
54+
55+
/**
56+
* 解码URL<br>
57+
* 将%开头的16进制表示的内容解码。
58+
*
59+
* @param url URL
60+
* @return 解码后的URL
61+
* @throws UtilException UnsupportedEncodingException
62+
*/
63+
public String urlDecode(String url) throws UtilException {
64+
if (StringUtil.isEmpty(url)) {
65+
return url;
66+
}
67+
try {
68+
return URLDecoder.decode(url, Constants.DEFAULT_ENCODING.displayName());
69+
} catch (UnsupportedEncodingException e) {
70+
throw new RuntimeException("Unsupported encoding", e);
71+
}
72+
}
5273
}

0 commit comments

Comments
 (0)