2013.11.22 ——— URL参数解析

2013.11.22 ——— URL参数解析

参考:[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 来解析。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值