Java URLEncoder and URLDecoder Class Examples

In this post, we will learn how to encode and decode URLs in Java with examples.

URLEncoder Class Overview

A URLEncoder is a Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIMEformat.
When encoding a String, the following rules apply:
  • The alphanumeric characters "a" through"z", "A" through"Z" and "0" through "9" remain the same.
  • The special characters ".","-", "*", and"_" remain the same.
  • The space character " " is converted into a plus sign "+".
  • All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string"%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.

URLEncoder Class Example

The URLEncoder utility class provides a encode() method which translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.
The below program demonstrate the usage of encode() method to encode URL - "https://www.javaguides.net/search?q=core+java+tutorial":
package net.javaguides.network;

import java.io.UnsupportedEncodingException;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class URLEncoderExample {

    public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException {
        // base URL 
        String baseurl = "https://www.javaguides.net/search?q=";

        // String to be encoded 
        String query = "core+java+tutorial";

        System.out.println("URL without encoding :");
        URL url = new URL(baseurl + query);
        System.out.println(url);

        // encode() method 
        System.out.println("URL after encoding :");
        url = new URL(baseurl + URLEncoder.encode(query, "UTF-8"));
        System.out.println(url);
    }
}
Output:
URL without encoding :
https://www.javaguides.net/search?q=core+java+tutorial
URL after encoding :
https://www.javaguides.net/search?q=core%2Bjava%2Btutorial

URLDecoder Class Overview

A URLDecoder is a utility class for HTML form decoding. This class contains static methods for decoding a String from the application/x-www-form-urlencoded MIME format.
The following rules are applied in the conversion:
  • The alphanumeric characters "a" through"z", "A" through"Z" and "0" through "9" remain the same.
  • The special characters ".","-", "*", and"_" remain the same.
  • The plus sign "+" is converted into a space character " ".
  • A sequence of the form "%xy" will be treated as representing a byte where xy is the two-digit hexadecimal representation of the 8 bits. Then, all substrings that contain one or more of these byte sequences consecutively will be replaced by the character(s) whose encoding would result in those consecutive bytes. The encoding scheme used to decode these characters may be specified, or if unspecified, the default encoding of the platform will be used.

URLDecoder Class Example

URLDecoder class provides decode() method which decodes an application/x-www-form-urlencoded string using a specific encoding scheme.
The below program demonstrate the usage of the decode() method:
package net.javaguides.network;

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

public class URLDecoderExample {

    public static void main(String[] args) throws UnsupportedEncodingException, MalformedURLException {
        // base URL
        String baseurl = "https://www.javaguides.net/search?q=";

        // String to be encoded
        String query = "core+java+tutorial";

        System.out.println("URL without encoding :");
        URL url = new URL(baseurl + query);
        System.out.println(url);

        // encode() method
        System.out.println("URL after encoding :");

        String encodedStr = URLEncoder.encode(query, "UTF-8");
        url = new URL(baseurl + encodedStr);
        System.out.println(url);

        // decode url process
        System.out.println("Encoded URL :");
        System.out.println(baseurl + encodedStr);

        // decode() method
        System.out.println("Decoded URL :");
        String decode = URLDecoder.decode(encodedStr, "UTF-8");
        System.out.println(baseurl + decode);
    }
}
Output:
URL without encoding :
https://www.javaguides.net/search?q=core+java+tutorial
URL after encoding :
https://www.javaguides.net/search?q=core%2Bjava%2Btutorial
Encoded URL :
https://www.javaguides.net/search?q=core%2Bjava%2Btutorial
Decoded URL :
https://www.javaguides.net/search?q=core+java+tutorial








Comments