URL Encoding/Decoding using Base64 in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Sample URL:
https://www.geeksforgeeks.org/
encoded URL:
aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv

Program 2: URL decoding using Base64 class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

encoded URL:
aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv
actual String:
https://www.geeksforgeeks.org/

References:



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.