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:
URLEncoder.encode(String, String)
:- First argument: The string (URL or part of it) to encode.
- Second argument: The character encoding (e.g.,
UTF-8
).
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.