如何在Java中进行URL解码?

问题:

 在Java中,我想转换这个:

https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type

 为此:

https://mywebsite/docs/english/site/mybook.do&request_type

 这是我到目前为止

class StringUTF 
{
    public static void main(String[] args) 
    {
        try{
            String url = 
               "https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do" +
               "%3Frequest_type%3D%26type%3Dprivate";

            System.out.println(url+"Hello World!------->" +
                new String(url.getBytes("UTF-8"),"ASCII"));
        }
        catch(Exception E){
        }
    }
}

但它不行。这些%3A%2F格式是什么,如何转换?

回答:

这与字符编码(如UTF-8或ASCII)无关。你那里的字符串是URL encoded。这种编码与字符编码完全不同。
尝试这样的东西:

String result = java.net.URLDecoder.decode(url, "UTF-8");

请注意,character encoding(如UTF-8或ASCII)是确定字符到原始字节的映射。有关字符编码的介绍,请参阅this article

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: How to do URL decoding in Java?

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

61 − 55 =