百度找了好久,索性自己搞一个
废话不多说直接上代码
public class UrlParamAnalysis {/*** 解析出url请求的路径,包括页面* * @param strURL url地址* @return url路径*/public static String UrlPage(String strURL) {String strPage = null;String[] arrSplit = null;strURL = strURL.trim().toLowerCase();arrSplit = strURL.split("[?]");if (strURL.length() > 0) {if (arrSplit.length > 1) {if (arrSplit[0] != null) {strPage = arrSplit[0];}}}return strPage;}/*** 去掉url中的路径,留下请求参数部分* * @param strURL url地址* @return url请求参数部分*/private static String TruncateUrlPage(String strURL) {String strAllParam = null;String[] arrSplit = null;arrSplit = strURL.split("[?]");if (strURL.length() > 1) {if (arrSplit.length > 1) {if (arrSplit[1] != null) {strAllParam = arrSplit[1];}}}return strAllParam;}public static Map<String, String> uRLRequest2(String URL) {Map<String, String> mapRequest = new HashMap<String, String>();String[] arrSplit = null;String strUrlParam = TruncateUrlPage(URL);if (strUrlParam == null) {return mapRequest;}// 每个键值为一组arrSplit = strUrlParam.split("[&]");for (String strSplit : arrSplit) {String[] arrSplitEqual = null;arrSplitEqual = strSplit.split("[=]", 2);// 解析出键值if (arrSplitEqual.length > 1) {// 正确解析mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);} else {if (arrSplitEqual[0] != "") {// 只有参数没有值,不加入mapRequest.put(arrSplitEqual[0], "");}}}return mapRequest;}
}
public class UrlParamAnalysisTest {public static void main(String[] args) {String url = "http://www.baidu.com?aa=q1222===&bb=pojwqde=&cc=vseefa=a=a";Map<String, String> stringStringMap = UrlParamAnalysis.uRLRequest2(url);System.out.println(JSON.toJSONString(stringStringMap));// 如果需要解析想直接用的话,前面必须有? ,或者自己改造下方法String baseUrl = "?data=vz9I/ZVKD+2h1lVBEXEeWCOCeSAmg2QhuRh5eChw==&partnerId=HSYC001&salt=/+9hPX5QSRl++Z2n9X5p7e==&sign=c/==×tamp=1685072690578";Map<String, String> stringStringMap2 = UrlParamAnalysis.uRLRequest2(baseUrl);System.out.println(JSON.toJSONString(stringStringMap2));}
}