Skip to content
Related Articles

Related Articles

URL Encoding/Decoding using Base64 in Java
  • Last Updated : 30 Jul, 2019

Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss.(Base 64 format reference).

The URL encoding is the same as Basic encoding the only difference is that it encodes or decodes the URL and Filename safe Base64 alphabet and does not add any line separation.

URL encoding

String encodedURL = Base64.getUrlEncoder()
                       .encodeToString(actualURL_String.getBytes());

Explanation: In above code we called Base64.Encoder using getUrlEncoder() and then get the encoded URLstring by passing the byte value of actual URL in encodeToString() method as parameter.

URL decoding



byte[] decodedURLBytes = Base64.getUrlDecoder().decode(encodedURLString);

String actualURL= new String(decodedURLBytes);

Explanation: In above code we called Base64.Decoder using getUrlDecoder() and then decoded the URL string passed in decode() method as parameter then convert return value to actual URL.

Below programs illustrate the Encoding and Decoding URL in Java:

Program 1: URL encoding using Base64 class.




// Java program to demonstrate
// URL encoding using Base64 class
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // create a sample url String to encode
        String sampleURL = "https:// www.geeksforgeeks.org/";
  
        // print actual URL String
        System.out.println("Sample URL:\n"
                           + sampleURL);
  
        // Encode into Base64 URL format
        String encodedURL = Base64.getUrlEncoder()
                                .encodeToString(sampleURL.getBytes());
  
        // print encoded URL
        System.out.println("encoded URL:\n"
                           + encodedURL);
    }
}
Output:
Sample URL:
https://www.geeksforgeeks.org/
encoded URL:
aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv

Program 2: URL decoding using Base64 class.




// Java program to demonstrate
// Decoding Basic Base 64 format to String
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // create a encoded URL to decode
        String encoded = "aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv";
  
        // print encoded URL
        System.out.println("encoded URL:\n"
                           + encoded);
  
        // decode into String URL from encoded format
        byte[] actualByte = Base64.getUrlDecoder()
                                .decode(encoded);
  
        String actualURLString = new String(actualByte);
  
        // print actual String
        System.out.println("actual String:\n"
                           + actualURLString);
    }
}
Output:
encoded URL:
aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv
actual String:
https://www.geeksforgeeks.org/

References:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :