/*
* Copyright 2012 The EGF Co,. Ltd.
* site: http://www.egfit.com
* file: $Id$
* created at:2012-3-7
*/
package com.egf.common.util;
import java.util.HashMap;
import java.util.Map;
/**
* @author xh
* @version $Revision: 1.1 $
* @since 0.1
*/
public class ResolveUrl {
/**
* 解析出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;
strURL = strURL.trim().toLowerCase();
arrSplit = strURL.split("[?]");
if (strURL.length() > 1) {
if (arrSplit.length > 1) {
if (arrSplit[1] != null) {
strAllParam = arrSplit[1];
}
}
}
return strAllParam;
}
/**
* 解析出url参数中的键值对 如 "index.jsp?Action=del&id=123",解析出Action:del,id:123存入map中
*
* @param URL
* url地址
* @return url请求参数部分
*/
public static Map<String, String> URLRequest(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("[=]");
// 解析出键值
if (arrSplitEqual.length > 1) {
// 正确解析
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
} else {
if (arrSplitEqual[0] != "") {
// 只有参数没有值,不加入
mapRequest.put(arrSplitEqual[0], "");
}
}
}
return mapRequest;
}
public static void main(String[] args) {
String url = "http://localhost:8080/portal/background/zdgzry!usearch?Entity.root.class=com.egf.qingbao.jzyp.entities.YpZdgzry&Entity.root.alias=o&o.yxx.value=0&Entity.forward=search&Entity.listStyle=zdgzry&cdid=45&Entity.url=/background/zdgzry!usearch&listForm.orderBy=o.tssj&listForm.order=desc&cdid=45&name=webmaster&dwdm=010000";
String strRequestKeyAndValues = "";
Map<String, String> mapRequest = ResolveUrl.URLRequest(url);
for (String strRequestKey : mapRequest.keySet()) {
String strRequestValue = mapRequest.get(strRequestKey);
strRequestKeyAndValues += "key:" + strRequestKey + ",Value:" + strRequestValue + ";";
}
System.out.println(strRequestKeyAndValues);
}
}
* Copyright 2012 The EGF Co,. Ltd.
* site: http://www.egfit.com
* file: $Id$
* created at:2012-3-7
*/
package com.egf.common.util;
import java.util.HashMap;
import java.util.Map;
/**
* @author xh
* @version $Revision: 1.1 $
* @since 0.1
*/
public class ResolveUrl {
/**
* 解析出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;
strURL = strURL.trim().toLowerCase();
arrSplit = strURL.split("[?]");
if (strURL.length() > 1) {
if (arrSplit.length > 1) {
if (arrSplit[1] != null) {
strAllParam = arrSplit[1];
}
}
}
return strAllParam;
}
/**
* 解析出url参数中的键值对 如 "index.jsp?Action=del&id=123",解析出Action:del,id:123存入map中
*
* @param URL
* url地址
* @return url请求参数部分
*/
public static Map<String, String> URLRequest(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("[=]");
// 解析出键值
if (arrSplitEqual.length > 1) {
// 正确解析
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
} else {
if (arrSplitEqual[0] != "") {
// 只有参数没有值,不加入
mapRequest.put(arrSplitEqual[0], "");
}
}
}
return mapRequest;
}
public static void main(String[] args) {
String url = "http://localhost:8080/portal/background/zdgzry!usearch?Entity.root.class=com.egf.qingbao.jzyp.entities.YpZdgzry&Entity.root.alias=o&o.yxx.value=0&Entity.forward=search&Entity.listStyle=zdgzry&cdid=45&Entity.url=/background/zdgzry!usearch&listForm.orderBy=o.tssj&listForm.order=desc&cdid=45&name=webmaster&dwdm=010000";
String strRequestKeyAndValues = "";
Map<String, String> mapRequest = ResolveUrl.URLRequest(url);
for (String strRequestKey : mapRequest.keySet()) {
String strRequestValue = mapRequest.get(strRequestKey);
strRequestKeyAndValues += "key:" + strRequestKey + ",Value:" + strRequestValue + ";";
}
System.out.println(strRequestKeyAndValues);
}
}