Tools Coverters

Mastering URL Decoding in Java - A Step-by-Step Tutorial

Mastering URL Decoding in Java - A Step-by-Step Tutorial Image

Published Date: September 9, 2022

Updated Date: September 9, 2022

In order to encode a string or a URL in Java, you can use the JavaURLEncoder method.

Java provides a URLEncoder class for encoding any string or URL. By default, the URLEncoder class in Java uses the UTF-8 format.

Similarly, if you want to decode a string in Java, you can do the same using the JavaURLDecoder class. The default decoding format is UTF-8

In frameworks like Dropwizard or Spring, the query strings and the form parameters are already decoded for you by the framework. However, if you are building any custom or stand alone java application, you can make use of the URLDecoder class.

How to encode a string or a URL in Java using the URLEncoder class

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

class URLEncodingExample {

    // Method to encode a string value using `UTF-8` encoding scheme
    private static String encodeValue(String value) {
        try {
            return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    public static void main(String[] args) {
        String baseUrl = "https://www.google.com/search?q=";

        String query = "Tööls Cönverters URLEncoder@Java";
        String encodedQuery = encodeValue(query); // Encoding a query string

        String completeUrl = baseUrl + encodedQuery;
        System.out.println(completeUrl);
    }
}

Output:

# Output
https://www.google.com/search?q=T%C3%B6%C3%B6ls%20C%C3%B6nverters%20xyz%20URLEncoder%40Java

You can also click this URL encoded link and see the results yourself Tööls Cönverters xyz URLEncoder@Java

Another interesting to note about the URLEncoder class in Java is that it encodes the space character as a + v/s other languages that encode the space character into %20. You can check out this StackOverflow discussion to understand the reasoning for character encoding of space as + v/s %20

If you note the code we wrote above

URLEncoder.encode(value, StandardCharsets.UTF_8.toString())

We are passing the format in which we want the encoding to happen. Here we are exclusively using UTF-8 format. You can choose to add another format that the function supports.

An important thing to note is that if you are URL Encoding in JAVA, and you pass in a format that is not supported or you try to create your own word as the 2nd argument to encode. You will receive an UnsupportedEncodingException from the compiler.

How to decode a string or a URL in Java using the URLDecoder class

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

class URLDecodingExample {

    // Method to encode a string value using `UTF-8` encoding scheme
    private static String decodeValue(String value) {
        try {
            return URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    public static void main(String[] args) {

        String query = "T%C3%B6%C3%B6ls%20C%C3%B6nverters%20xyz%20URLDecoder%40Java";
        String encodedQuery = decodeValue(query); // Decoding a query string

        String completeUrl = encodedQuery;
        System.out.println(completeUrl);
    }
}

Output:

# Output
Tööls Cönverters URLDecoder@Java

You can also click this URL encoded link and see the results yourself Tööls Cönverters xyz URLEncoder@Java

Related Decoding techniques:

Related Encoding techniques:

There are many applications that require you to encode your URL into a format that the systems can understand. If you wish to encode a URL, check out our free online URL Encoder.

Some systems communicate with encoded URLs, so you may need to decode a URL. If you want to decode a URL online, we have our free online URL Decoder.