2013.11.22 ——— URL参数解析
参考:[url]https://gist.github.com/hellojinjie/5651980[/url]
URL 参数解析方法:
1、httpclient org.apache.http.client.utils.URLEncodedUtils
2、jettyUtil org.eclipse.jetty.util.UrlEncoded
3、tomcat org.apache.catalina.util.RequestUtil
4、regex 正则表达式
5、split 使用String 的split 方法对 URL 进行分割,然后用equals 匹配对应的 参数
前三者是 httpclient, jetty, tomcat 使用的 URL 解析工具。Split 方法是最简单 也是最直观的解析方法,regex 则使用了正则表达式去匹配参数
ettyUtil 解析URL的性能在五种中最高,如果我们在项目中需要解析 URL ,应该 尽可能的考虑使用 jettyUtil 来解析。
参考:[url]https://gist.github.com/hellojinjie/5651980[/url]
URL 参数解析方法:
1、httpclient org.apache.http.client.utils.URLEncodedUtils
URLEncodedUtils.parse(query, Charset.forName("UTF-8"));
2、jettyUtil org.eclipse.jetty.util.UrlEncoded
MultiMap<String> values = new MultiMap<String>();
UrlEncoded.decodeTo(query, values, "UTF-8", 1000);
3、tomcat org.apache.catalina.util.RequestUtil
Map<String, String> values = new HashMap<String, String>();
RequestUtil.parseParameters(values, query, "UTF-8");
4、regex 正则表达式
String u = URLDecoder.decode(url, "UTF-8");
for (String s : parameters) {
Pattern p = Pattern.compile(s + "=([^&]*)(&|$)");
Matcher m = p.matcher(u);
if (m.find()) {
m.group(1);
}
}
5、split 使用String 的split 方法对 URL 进行分割,然后用equals 匹配对应的 参数
String u = URLDecoder.decode(url, "UTF-8");
for (String s : parameters) {
String[] a = new String[100];
if (u.indexOf(s) != -1) {
a = (u.substring(u.indexOf(s))).split("&");
a[0].split("=");
}
}
前三者是 httpclient, jetty, tomcat 使用的 URL 解析工具。Split 方法是最简单 也是最直观的解析方法,regex 则使用了正则表达式去匹配参数
ettyUtil 解析URL的性能在五种中最高,如果我们在项目中需要解析 URL ,应该 尽可能的考虑使用 jettyUtil 来解析。