How to do URL decoding in Java?

前端 未结 9 1413
轮回少年
轮回少年 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:14

    The string you've got is in application/x-www-form-urlencoded encoding.

    Use URLDecoder to convert it to Java String.

    URLDecoder.decode( url, "UTF-8" );
    
    0 讨论(0)
  • 2020-11-22 05:17

    This has been answered before (although this question was first!):

    "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)."

    As URL class documentation states:

    The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL().

    The URLEncoder and URLDecoder classes can also be used, but only for HTML form encoding, which is not the same as the encoding scheme defined in RFC2396.

    Basically:

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

    will give you:

    https://mywebsite/docs/english/site/mybook.do?request_type
    
    0 讨论(0)
  • 2020-11-22 05:22
    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")); 
    
        } 
    
    }
    

    You can select your method wisely :)

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

    %3A and %2F are URL encoded characters. Use this java code to convert them back into : and /

    String decoded = java.net.URLDecoder.decode(url, "UTF-8");
    
    0 讨论(0)
  • 2020-11-22 05:23
     try {
            String result = URLDecoder.decode(urlString, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-11-22 05:23

    Using java.net.URI class:

    public String getDecodedURL(String encodedUrl) {
        try {
            URI uri = new URI(encodedUrl);
            return uri.getScheme() + ":" + uri.getSchemeSpecificPart();
        } catch (Exception e) {
            return "";
        }
    }
    

    Please note that exception handling can be better, but it's not much relevant for this example.

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