0

我会解码一个 URL,但仅限于 % 编码。我想保持非 % 编码不变:

例如

url = 
http%3A%2F%2Fwww.domain.com%2FNews%2Fpolitics%2FMPs%2Brisk%2Blosing%2Bparliamentary%2Bimmunity%2B%2F-%2F1064%2F1395978%2F-%2Flvylki%2F-%2Findex.html

URLDecoder.decode(url, "UTF-8") = http://www.domain.com/News/politics/MPs+risk+losing+parliamentary+immunity+/-/1064/1395978/-/lvylki/-/index.html

但是,如果:

url = http://www.domain.com/News/politics/MPs+risk+losing+parliamentary+immunity+/-/1064/1395978/-/lvylki/-/index.html

URLDecoder.decode(url, "UTF-8") = http://www.domain.com/News/politics/MPs risk losing parliamentary immunity /-/1064/1395978/-/lvylki/-/index.html

我想:

URLDecoder.decode(url, "UTF-8") = http://www.domain.com/News/politics/MPs+risk+losing+parliamentary+immunity+/-/1064/1395978/-/lvylki/-/index.html

如何避免URLDecoder.decode()将 + 解码为“”。一般来说,我想保留非 % 编码的字符串,即只转换 % 编码。

4

1 回答 1

1
URLDecoder.decode(url.replaceAll("\\+", "%2B"))
于 2012-04-29T20:39:04.313 回答