Of course! In Java, there are two primary ways to decode a URL, depending on what part of the URL you need to decode.

java.net.URLDecoder: Best for decoding the query string (the part after the ).java.net.URI: A more modern and robust way to decode any part of a URL, including the path, query, and fragment.
Let's break down both methods with clear examples.
Method 1: java.net.URLDecoder (For Query Strings)
URLDecoder is designed specifically for the application/x-www-form-urlencoded format, which is used in the query string of a URL.
Key Points:
- It decodes a
Stringwhere spaces are represented by and special characters are represented by%XX(hex codes). - It's the correct tool for decoding the key-value pairs in a URL's query string.
Example: Decoding a Query String
Let's say you have a URL like: https://example.com/search?q=java%20tutorial&page=2&query=hello%20world

We want to decode the query part: q=java%20tutorial&page=2&query=hello%20world
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class UrlDecoderExample {
public static void main(String[] args) {
// The query string part of a URL
String encodedQuery = "q=java%20tutorial&page=2&query=hello%20world";
try {
// Decode the entire query string
String decodedQuery = URLDecoder.decode(encodedQuery, StandardCharsets.UTF_8.name());
System.out.println("Original Encoded Query: " + encodedQuery);
System.out.println("Decoded Query: " + decodedQuery);
// Output: Decoded Query: q=java tutorial&page=2&query=hello world
// Now, let's parse it into a map of parameters
Map<String, String> params = parseQueryParams(decodedQuery);
System.out.println("\nParsed Parameters:");
params.forEach((key, value) -> System.out.println(key + " = " + value));
// Output:
// Parsed Parameters:
// q = java tutorial
// page = 2
// query = hello world
} catch (UnsupportedEncodingException e) {
// This exception is thrown if the specified encoding is not supported.
// UTF-8 is guaranteed to be supported, so this is unlikely to happen.
System.err.println("Error: Unsupported encoding - " + e.getMessage());
}
}
/**
* Helper method to parse a decoded query string into a map of parameters.
* @param query The decoded query string (e.g., "key1=value1&key2=value2")
* @return A map of parameter keys to values.
*/
public static Map<String, String> parseQueryParams(String query) {
Map<String, String> params = new HashMap<>();
if (query == null || query.isEmpty()) {
return params;
}
String[] pairs = query.split("&");
for (String pair : pairs) {
String[] keyValue = pair.split("=", 2); // Split into 2 parts at the first '='
if (keyValue.length == 2) {
String key = keyValue[0];
String value = keyValue[1];
params.put(key, value);
} else {
// Handle keys without values (e.g., "flag")
params.put(keyValue[0], "");
}
}
return params;
}
}
Method 2: java.net.URI (The Modern, Robust Approach)
The URI class is part of Java's more modern networking API and is the recommended way to handle all parts of a URL. It's more robust because it separates the URL into its logical components (scheme, host, path, query, etc.) before you decode them.
Key Points:
- It doesn't decode to a space in the path, only in the query string, which is correct according to the URL specification.
- It provides clear methods to get each part of the URL:
getPath(),getQuery(),getFragment(). - You should use
URI.getRawPath()to get the encoded path andURI.getPath()to get the decoded path. The same applies to the query string.
Example: Decoding a Full URL
Let's use a more complex URL: https://my-site.com/api/data/users/john%20doe?name=John%20Doe&role=admin#section%201

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
public class UriDecoderExample {
public static void main(String[] args) {
String urlString = "https://my-site.com/api/data/users/john%20doe?name=John%20Doe&role=admin#section%201";
try {
// 1. Create a URI object from the URL string
URI uri = new URI(urlString);
// 2. Get the encoded and decoded components
System.out.println("Original URL: " + urlString);
System.out.println("---------------------------------");
// --- Scheme ---
System.out.println("Scheme: " + uri.getScheme()); // https
// --- Host ---
System.out.println("Host: " + uri.getHost()); // my-site.com
// --- Path ---
// getRawPath() returns the path with %XX sequences intact
String rawPath = uri.getRawPath();
System.out.println("Encoded Path: " + rawPath); // /api/data/users/john%20doe
// getPath() returns the path with %XX sequences decoded
String decodedPath = uri.getPath();
System.out.println("Decoded Path: " + decodedPath); // /api/data/users/john doe
// --- Query String ---
// getRawQuery() returns the query with %XX sequences intact
String rawQuery = uri.getRawQuery();
System.out.println("\nEncoded Query: " + rawQuery); // name=John%20Doe&role=admin
// getQuery() returns the query with %XX sequences decoded
String decodedQuery = uri.getQuery();
System.out.println("Decoded Query: " + decodedQuery); // name=John Doe&role=admin
// --- Fragment ---
// getRawFragment() returns the fragment with %XX sequences intact
String rawFragment = uri.getRawFragment();
System.out.println("\nEncoded Fragment: " + rawFragment); // section%201
// getFragment() returns the fragment with %XX sequences decoded
String decodedFragment = uri.getFragment();
System.out.println("Decoded Fragment: " + decodedFragment); // section 1
} catch (URISyntaxException e) {
System.err.println("Invalid URL syntax: " + e.getMessage());
}
}
}
Comparison: URLDecoder vs. URI
| Feature | java.net.URLDecoder |
java.net.URI |
|---|---|---|
| Primary Use Case | Decoding the query string (?key=value). |
Parsing and decoding any part of a URL. |
| How it Works | Decodes a single String. |
Parses the entire URL into components first. |
| Handling | Always decodes to a space. | Correctly decodes to a space only in the query string. In the path, remains . |
| Robustness | Less robust. Can fail on malformed URLs. | More robust. Correctly separates URL components. |
| Recommendation | Use only if you are certain you are decoding a query string and nothing else. | Highly Recommended. It's the standard, safer, and more correct way to handle URL decoding in Java. |
Important Consideration: Character Encoding
URLs must be encoded using a specific character set. UTF-8 is the de-facto standard and the recommended encoding for all modern web applications.
Both URLDecoder.decode() and the URI class constructor/decoder methods use a character encoding. Always specify StandardCharsets.UTF_8 to avoid issues with different system default encodings.
// Correct and safe String decoded = URLDecoder.encode(encodedString, StandardCharsets.UTF_8.name()); // Potentially unsafe, relies on the platform's default encoding // String decoded = URLDecoder.encode(encodedString, "ISO-8859-1"); // Example of another encoding
Summary
- For decoding just the query string,
URLDecoderis a simple, viable option. - For any other scenario, or for a more robust and correct solution, always prefer
java.net.URI. It correctly handles the different rules for encoding in the path, query, and fragment of a URL.
