我需要解碼包含查詢字符串的URI;預期輸入/輸出行爲是類似如下:在Java中解碼URI查詢字符串
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嘗試過,但似乎這樣在上面的例子中我留下了「別名= POS &名字=到查詢字符串解碼Foo + A & B = C & LastName = Bar「因此,不管」&「是查詢分隔符還是查詢組件中的字符都不明確。
編輯:我只是想URI.getRawQuery(),並沒有做編碼,這樣我就可以用&
分割查詢字符串,但後來我該怎麼辦? Javascript有decodeURIComponent,我似乎無法在Java中找到相應的方法。
有什麼建議嗎?我不想使用任何新的庫。
的答案既然你不想URLDecoder功能的輔助類爲了引入新的庫,我可以問你在哪個環境中接收這些URI? – stacker 2010-04-13 19:39:16