This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: How do I decode urls in Java

I want to use Java to make the following string garbled

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

It’s resolved in this form

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

This is what I’ve written so far

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){
        }
    }
}
Copy the code

But the above method doesn’t work. What do formats like %3A and %2F mean? How do I convert them?

Answer a

This has nothing to do with character encodings such as UTF-8 or ASCII. Your string is URL-encoded. This encoding is completely different from character encoding.

Try the following:

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
}
Copy the code

10 in Java API to add the direct support for character set, which means that don’t need to capture UnsupportedEncodingException anomalies

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

Answer two

You should use java.net.URI to do this, because the URldecker class decodes X-www-form-urlencoded, which is for form data.

As described in the URL documentation

The recommended way to manage URL encoding and decoding is to use URIs. And use toURI() and uri.tourl () to convert between classes.

The sample

String url = "https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type";
System.out.println(new java.net.URI(url).getPath());
Copy the code

Answer three

Here is the URL decoder I wrote:

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

public class URLDecoding { 

    String decoded = "";

    public String decodeMethod(String url) throws UnsupportedEncodingException
    {
        decoded = java.net.URLDecoder.decode(url, "UTF-8"); 
        return  decoded;
//"You should use java.net.URI to do this, as the URLDecoder class does x-www-form-urlencoded decoding which is wrong (despite the name, it's for form data)."
    }

    public String getPathMethod(String url) throws URISyntaxException 
    {
        decoded = new java.net.URI(url).getPath();  
        return  decoded; 
    }

    public static void main(String[] args) throws UnsupportedEncodingException, URISyntaxException 
    {
        System.out.println(" Here is your Decoded url with decode method : "+ new URLDecoding().decodeMethod("https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type")); 
        System.out.println("Here is your Decoded url with getPath method : "+ new URLDecoding().getPathMethod("https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest")); }}Copy the code

The article translated from Stack Overflow:stackoverflow.com/questions/6…