shenyijian
2024-12-02 eed72349910ec181532e5f7ce13677788a769d21
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cn.gistack.by.nettyUpload.utils;
 
import io.netty.handler.codec.http.HttpRequest;
import org.apache.commons.lang3.StringUtils;
 
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
 
/**
 * URI 工具类
 * @author chenhaijian
 * @date 2020-05-21 16:43
 */
public class UriUtils {
 
    /**
     * 获取HttpRequest中的uri封装成URI对象
     * @param httpRequest
     * @return
     * @throws URISyntaxException
     */
    public static URI getURI(HttpRequest httpRequest) throws URISyntaxException {
        return new URI(httpRequest.uri());
    }
 
    /**
     * 获取URI对象中的参数
     * @param uri
     * @return
     */
    public static Map<String, String> getQueryParams(URI uri) {
        Map<String, String> params = new HashMap<>();
        String query = uri.getQuery();
        if (StringUtils.isNotBlank(query)) {
            String[] paramsItems = query.split("&");
            for (String item : paramsItems) {
                String[] kv = item.split("=");
                params.put(kv[0], kv[1]);
            }
        }
        return params;
    }
 
    /**
     * 获取HttpRequest中的 query params
     * @param httpRequest
     * @return
     */
    public static Map<String, String> getQueryParams(HttpRequest httpRequest) throws URISyntaxException {
        URI uri = getURI(httpRequest);
        return getQueryParams(uri);
    }
}