How to Encode and Decode URLs in Java

In Java, you can encode and decode URLs using the java.net.URLEncoder and java.net.URLDecoder classes. These classes handle encoding and decoding in compliance with the application/x-www-form-urlencoded MIME type.
Here’s how you can encode and decode URLs:

Code Example

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;

public class URLEncoderDecoderExample {

    public static void main(String[] args) {
        try {
            // The String to be encoded
            String url = "https://example.com/query?name=John Doe&age=25";

            // Encoding URL
            String encodedUrl = URLEncoder.encode(url, "UTF-8");
            System.out.println("Encoded URL: " + encodedUrl);

            // Decoding URL
            String decodedUrl = URLDecoder.decode(encodedUrl, "UTF-8");
            System.out.println("Decoded URL: " + decodedUrl);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace(); // Handle exception if unsupported encoding is provided
        }
    }
}

Explanation:

  1. Encoding:
    • The URLEncoder.encode() method encodes special characters in the URL to make it safe for transmission over the network.
    • UTF-8 is typically used as the charset.
  2. Decoding:
    • The URLDecoder.decode() method decodes the string back to its original format.

Sample Output:

If the input is:

https://example.com/query?name=John Doe&age=25

After encoding:

https%3A%2F%2Fexample.com%2Fquery%3Fname%3DJohn+Doe%26age%3D25

After decoding:

https://example.com/query?name=John Doe&age=25

Notes:

  • Replace spaces with + in the encoded string. This is because spaces are not typically allowed in URLs, and encoding replaces them.
  • Use "UTF-8" because it’s the most widely used and supports all Unicode characters.

How do I implement URL encoding and decoding using URLEncoder and URLDecoder in Java?

In Java, you can use the URLEncoder and URLDecoder classes to handle URL encoding and decoding. These classes are part of the java.net package and are often used to ensure that special characters in URLs are properly encoded so they can be safely transmitted over the web. For decoding, you can convert encoded URLs back to their original form.

Here’s how you can implement URL encoding and decoding:

1. Encoding a URL using URLEncoder

Encoding a URL involves replacing unsafe characters or special characters with a % followed by hexadecimal digits. For instance, a space will be replaced by %20.

package org.kodejava.net;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class UrlEncodingExample {
   public static void main(String[] args) {
      try {
         String url = "https://example.com/query?name=John Doe&city=New York";

         // Encode the URL
         String encodedUrl = URLEncoder.encode(url, StandardCharsets.UTF_8);

         System.out.println("Original URL: " + url);
         System.out.println("Encoded URL: " + encodedUrl);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

2. Decoding a URL using URLDecoder

Decoding a URL transforms it back to its original, human-readable form by replacing encoded sequences with their respective characters.

package org.kodejava.net;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class UrlDecodingExample {
   public static void main(String[] args) {
      try {
         String encodedUrl = "https%3A%2F%2Fexample.com%2Fquery%3Fname%3DJohn%2BDoe%26city%3DNew%2BYork";

         // Decode the URL
         String decodedUrl = URLDecoder.decode(encodedUrl, StandardCharsets.UTF_8);

         System.out.println("Encoded URL: " + encodedUrl);
         System.out.println("Decoded URL: " + decodedUrl);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Explanation of the Parameters:

  1. URLEncoder.encode(String, String):
    • First argument: The string (URL or part of it) to encode.
    • Second argument: The character encoding (e.g., UTF-8).
  2. URLDecoder.decode(String, String):
    • First argument: The encoded string to decode.
    • Second argument: The character encoding.

Both URLEncoder.encode and URLDecoder.decode require a character encoding parameter, which specifies how characters are encoded/decoded. It’s common to use UTF-8 as it is the standard encoding for the web.

Output Example:

Encoding Example:

  • Input: https://example.com/query?name=John Doe&city=New York
  • Encoded: https%3A%2F%2Fexample.com%2Fquery%3Fname%3DJohn%2BDoe%26city%3DNew%2BYork

Decoding Example:

  • Input: https%3A%2F%2Fexample.com%2Fquery%3Fname%3DJohn%2BDoe%26city%3DNew%2BYork
  • Decoded: https://example.com/query?name=John Doe&city=New York

Important Notes

  • Be sure to use the appropriate character encoding (e.g., UTF-8), as using the wrong one might result in garbled data.
  • URLEncoder encodes spaces as + (plus sign), conforming to application/x-www-form-urlencoded (often used in HTML form submissions). If you need to encode spaces as %20 (used in URLs), additional handling may be required.

This is how you can use URLEncoder and URLDecoder effectively for URL encoding and decoding in Java.

Differences between getEncoder() and getUrlEncoder() method of java.util.Base64 class

The java.util.Base64.getEncoder() and java.util.Base64.getUrlEncoder() methods in Java both return a Base64 encoder. However, they exhibit different behaviors mainly due to the encoding scheme they follow for handling URL and filename safe characters.

  • java.util.Base64.getEncoder(): This method returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme as per RFC 4648 Section 4.

Basic Base64 encoding does not handle URL and filename safe characters well. It uses + for 62, / for 63, and = for padding. These characters can cause problems in URLs.

An example of its usage.

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64EncoderExample {
    public static void main(String[] args) {
        Base64.Encoder encoder = Base64.getEncoder();
        String str = "https://www.google.com/search?q=Hello+World";
        String encodedString = encoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
        System.out.println("encodedString = " + encodedString);
    }
}

Output:

encodedString = aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g/cT1IZWxsbytXb3JsZA==
  • java.util.Base64.getUrlEncoder(): This method returns a Base64.Encoder that encodes in URL and filename safe type base64 scheme as per RFC 4648 Section 5.

URL and filename safe Base64 encoding replaces + with - , and / with _. It also omits padding characters. This makes it safer for use in URLs or file-system paths.

An example of its usage.

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64UrlEncoderExample {
    public static void main(String[] args) {
        Base64.Encoder urlEncoder = Base64.getUrlEncoder();
        String str = "https://www.google.com/search?q=Hello+World";
        String urlEncodedString = urlEncoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
        System.out.println("encodedString = " + urlEncodedString);
    }
}

Output:

encodedString = aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g_cT1IZWxsbytXb3JsZA==

So the choice between these two would depend on where you intend to use the encoded bytes. If it’s to be in a URL or a filepath, it’s recommended to use the getUrlEncoder() due to its url safe encoding scheme. For other use cases, the getEncoder() method should suffice.

Similar to the getEncoder() and getUrlEncoder() methods, the java.util.Base64 class provides the getDecoder() and getUrlDecoder() methods for Base64 decoding.

  • java.util.Base64.getDecoder(): This method returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme.

Example:

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64DecodeExample {
    public static void main(String[] args) {
        Base64.Decoder decoder = Base64.getDecoder();

        // "Hello, World!" in Base64
        String str = "SGVsbG8sIFdvcmxkIQ==";
        String decodedString = new String(decoder.decode(str), StandardCharsets.UTF_8);
        System.out.println("decodedString = " + decodedString);
    }
}

Output:

decodedString = Hello, World!
  • java.util.Base64.getUrlDecoder(): This method returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme.

Example:

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64UrlDecoderExample {
    public static void main(String[] args) {
        Base64.Decoder urlDecoder = Base64.getUrlDecoder();

        // "Hello, World!" in URL-safe Base64
        String urlSafeStr = "SGVsbG8sIFdvcmxkIQ";
        String decodedString = new String(urlDecoder.decode(urlSafeStr), StandardCharsets.UTF_8);
        System.out.println("decodedString = " + decodedString);
    }
}

Output:

decodedString = Hello, World!

The choice between these two would again depend on from where you are getting the encoded bytes. If it’s from a URL or a filepath, you would want to use getUrlDecoder(). For other use cases, the getDecoder() method should suffice.