How to do URL decoding in Java?

前端 未结 9 1414
轮回少年
轮回少年 2020-11-22 05:01

In Java, I want to convert this:

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

To thi

相关标签:
9条回答
  • 2020-11-22 05:30
    public String decodeString(String URL)
        {
    
        String urlString="";
        try {
            urlString = URLDecoder.decode(URL,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
    
            }
    
            return urlString;
    
        }
    
    0 讨论(0)
  • 2020-11-22 05:35

    This does not have anything to do with character encodings such as UTF-8 or ASCII. The string you have there is URL encoded. This kind of encoding is something entirely different than character encoding.

    Try something like this:

    try {
        String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
        // not going to happen - value came from JDK's own StandardCharsets
    }
    

    Java 10 added direct support for Charset to the API, meaning there's no need to catch UnsupportedEncodingException:

    String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8);
    

    Note that a character encoding (such as UTF-8 or ASCII) is what determines the mapping of characters to raw bytes. For a good intro to character encodings, see this article.

    0 讨论(0)
  • 2020-11-22 05:36

    I use apache commons

    String decodedUrl = new URLCodec().decode(url);
    

    The default charset is UTF-8

    0 讨论(0)
提交回复
热议问题