UrlParamParserUtils.java
1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.diligrp.cashier.shared.util;
import com.diligrp.cashier.shared.exception.PlatformServiceException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName UrlParamParserUtils.java
* @author dengwei
* @version 1.0.0
* @Description UrlParamParserUtils
* @date 2025-12-25 15:34
*/
public class UrlParamParserUtils {
public static Map<String, String> parseQueryParams(String url) {
try {
URI uri = new URI(url);
String query = uri.getQuery();
if (query == null || query.isEmpty()) {
return new HashMap<>();
}
Map<String, String> params = new HashMap<>();
String[] pairs = query.split("&");
for (String pair : pairs) {
String[] keyValue = pair.split("=", 2);
String key = URLDecoder.decode(keyValue[0], StandardCharsets.UTF_8);
String value = keyValue.length > 1 ? URLDecoder.decode(keyValue[1], StandardCharsets.UTF_8) : "";
params.put(key, value);
}
return params;
} catch (Exception e) {
throw new PlatformServiceException("Invalid URL");
}
}
}