欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取url中的query string

程序员文章站 2024-02-25 22:00:57
...

获取url中的query string

util方法:

/***
	 * convert request query string to map
	 * 
	 * @param queryString
	 * @return
	 */
	public static Map<String, Object> parseQueryString(String queryString) {
		if (ValueWidget.isNullOrEmpty(queryString)) {
			return null;
		}
		int index = queryString.indexOf("?");
		if (index != SystemHWUtil.NEGATIVE_ONE) {
			queryString = queryString.substring(index + 1);
		}
		
		Map<String, Object> argMap = new HashMap<String, Object>();
		String[] queryArr = queryString.split("&");
		for (int i = 0; i < queryArr.length; i++) {
			String string = queryArr[i];
			String keyAndValue[] = string.split("=", 2);
			if (keyAndValue.length != 2) {
				argMap.put(keyAndValue[0], EMPTY);
			} else {
				argMap.put(keyAndValue[0], keyAndValue[1]);
			}
		}
		return argMap;
	}

 

测试:

@Test
	public  void test_queryString(){
		String url="orderId=INTE2016031800025&orgId=90001001119";

		Map<String, Object> argMap =SystemHWUtil.parseQueryString(url);
		System.out.println(argMap);
	}

 运行结果:{orgId=90001001119, orderId=INTE2016031800025}

 

@Test
	public  void test_queryString(){
		String url="http://store.chanjet.com/order/startPay?orderId=INTE2016031701837&payType=chanpay&orgId=90001001032&callack= ";
		Map<String, Object> argMap =SystemHWUtil.parseQueryString(url);
		System.out.println(argMap);
	}

运行结果:{orgId=90001001032, callack= , payType=chanpay, orderId=INTE2016031701837}

 

 

 

 

相关标签: 获取querystring