Get parameter map from url string

A small utility method for converting the params in a String url into a map. The entry is to call the toMap method

/**
     * Remove the path in the url and leave the request parameter part
     * @param strURL url address
     * @return url request parameter part
     */
    private static String truncatePath(String strURL){
        if(StringUtils.isNotEmpty( strURL)){
            String[] arrSplit = strURL.trim().split("[?]");
            if(arrSplit.length > 1){
                return arrSplit[1];
            }
        }
        return null;
    }


    /**
     * Parsing url, get parameters
     * @param url
     * @return
     */
    private static Map<String, String> toMap(String url) {
        final Map<String, String> paramsMap = new LinkedHashMap<String, String>();
        if ((url = truncatePath(url)) != null && url.indexOf(PARAM_DEFINE) > -1) {
            Stream.of(url.split(PARAM_TOKENIZER)).forEach( str -> paramsMap.put(str.split(PARAM_DEFINE)[0], str.split(PARAM_DEFINE)[1]));
        }
        return paramsMap;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326182222&siteId=291194637