获取url中的query string

长孙朝明
2023-12-01

获取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}

 

 

 

 

 类似资料: