URL Encoding and Decoding using Java

In this tutorial, we will learn to encode and decode the given URL in Java using two utility class.

What is the URL?

URL or Uniform Resource Locator is a means to find resources on the Internet. It gives the address of the resource and tells us how to retrieve or communicate with it.

We use:

  • java.net.URLEncoder: It is used to perform HTML from encoding. This is a reliable and secure method to perform encoding.
    • All the alphabets, a-z, and A-Z remain the same.
    • The numbers ranging from ‘0’ to ‘9’ will remain the same.
    • (*),(_),(.),(-) are some of the special characters that will remain the same. Another special character such as space ” ” will be converted to a “+” sign.
    • Methods :
      • encode(): We define this method for URL encoding in Java. This method can either take one parameter, that is String that is needed to be encoded or two parameters, that is, String and encoding type. If one parameter is specified then function uses platform default encoding type.
  • java.net.URLDecoder: It is used to perform HTML form decoding. Its function is the opposite of the encoder utility class in a way that decodes the given string.
      • All the alphabets, a-z, and A-Z remain the same.
      • The numbers ranging from ‘0’ to ‘9’ will remain the same.
      • (*),(_),(.),(-) are some of the special characters that will remain the same. Another special character such as plus sign “+” will be converted to a ” ” sign.
      • All other characters are decoded using the encoding scheme specified.
  • Methods: decode(): We define this method for URL decoding in Java. Therefore, we make use of this method in our code as well.
  • This method can either take one parameter, that is String that is needed to be decoded or two parameters, that is, String and decoding type.
    • If only one parameter is specified then function uses platform default decoding type.
    Syntax :public static String decode(String s)- @Deprecated
    Parameters :
    s : encoded string to be decoded
    Syntax :public static String decode(String s,
                String enc)
                         throws UnsupportedEncodingException
    Parameters : 
    s : string to be decoded
    enc : encoding to be used
    Throws :
    UnsupportedEncodingException : If the specified encoding is not used

Main Method:

  • url1: String to be decoded
  • url2: String to be encoded
  • URLdecoder method (for URL decoding)- decodes the given string and throws an Exception if an error occurs. Therefore, we use try and catch here.

    Now, encoding can be done several times. Therefore we decode the URL to the simplest form. We use variable previous and variable decoded. Loop is run till previous equals decoded. Therefore, this loop ensures decoding is done effectively for the URL that has been encoded multiple times.

  • URLencoder method (for URL encoding)- encodes the given String and throws an Exception if an error occurs. Therefore, we use try and catch here.
import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;
 
import java.net.URLEncoder;
 
public class URLEncodingDecoding 
{ 
    public static void main(String[] args) { 
        String url1=" https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DM81wneSjQbA%26list%3DRDQPGmwRNkffc%26index%3D2"; 

        String url2="https://www.youtube.com/watch?v=M81wneSjQbA&list=RDQPGmwRNkffc&index=2"; 

        String string_after_decoding = URLdecoder(url1); System.out.println("Decoded URL: "+string_after_decoding); 

        String string_after_encoding = URLencoder(url2); System.out.println("Encoded URL: "+string_after_encoding); 
        
    }
    public static String URLdecoder(String url) { 

        try { 

            String initial=""; 

            String final_string =url; 
            
            while(!initial.equals(final_string)) { 
                
                initial=final_string; 

                final_string=URLDecoder.decode( final_string, "UTF-8" ); 
                
            } return final_string; 
            
        } catch (UnsupportedEncodingException e) { 

                return "Issue while decoding" +e.getMessage(); 
                
            } 
        
    } 
    public static String URLencoder(String url) { 
        try { 

            String final_string=URLEncoder.encode( url, "UTF-8" );

            return final_string; 
            
        } 
        catch (UnsupportedEncodingException e) { 

            return "Issue while encoding" +e.getMessage(); 
            
        } 
        
    } 
    
}

The output of the above code will be:

Decoded URL:  https://www.youtube.com/watch?v=M81wneSjQbA&list=RDQPGmwRNkffc&index=2
Encoded URL: https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DM81wneSjQbA%26list%3DRDQPGmwRNkffc%26index%3D2

Therefore, we have successfully done URL encoding and decoding in Java.

Also read: How to deal with the diamond problem in Java

Leave a Reply

Your email address will not be published.