java – 解码包含百分比的字符串(%)

我正在尝试解码包含(%)百分比的字符串,它正在抛出异常

Exception:URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "%&"

我的代码:

public class DecodeCbcMsg {

    public static void main(String[] args) throws UnsupportedEncodingException 
    {
        String msg="Hello%%&&$$";
        String strTMsg = URLDecoder.decode(msg,"UTF-8");
        System.out.println(strTMsg);
    }

最佳答案 看起来你的字符串编码不正确……

也许你应该确保它首先被正确编码?

例如,%的编码字符表示是%…

所以请尝试解码Hello %% \u0026\u0026 $$,看看你得到了什么:)

点赞