用Java解码URI查询字符串 [英] Decoding URI query string in Java

查看:304
本文介绍了用Java解码URI查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解码包含查询字符串的URI;预期的输入/输出行为类似于以下内容:

I need to decode a URI that contains a query string; expected input/output behavior is something like the following:

abstract class URIParser
{       
    /** example input: 
      * something?alias=pos&FirstName=Foo+A%26B%3DC&LastName=Bar */
    URIParser(String input) { ... }
    /** should return "something" for the example input */
    public String getPath(); 
    /** should return a map 
      * {alias: "pos", FirstName: "Foo+A&B=C", LastName: "Bar"} */
    public Map<String,String> getQuery();
}

我尝试过使用 java.net.URI ,但它似乎解码查询字符串所以在上面的例子我留下了alias = pos& FirstName = Foo + A& B = C& LastName = Bar,所以有一个&的含糊不清是一个查询分隔符,或者是查询组件中的一个字符。

I've tried using java.net.URI, but it seems to decode the query string so in the above example I'm left with "alias=pos&FirstName=Foo+A&B=C&LastName=Bar" so there is ambiguity whether a "&" is a query separator or is a character in a query component.

编辑:我刚试过 URI.getRawQuery()它没有做编码,所以我可以用& 拆分查询字符串,但那我该怎么办? Javascript有 decodeURIComponent ,我似乎无法找到相应的方法在Java。

I just tried URI.getRawQuery() and it doesn't do the encoding, so I can split the query string with a &, but then what do I do? Javascript has decodeURIComponent, I can't seem to find the corresponding method in Java.

有什么建议吗?我不想使用任何新库。

Any suggestions? I would prefer not to use any new libraries.

推荐答案

参见class URLDecoder

这篇关于用Java解码URI查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆