URL Decoding in Java Made Simple
Problem:
You wish to convert a URL-encoded string like this:
https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type
To a decoded form like this:
https://mywebsite/docs/english/site/mybook.do&request_type
Solution:
The encoding you're dealing with here is not character encoding (e.g., UTF-8 or ASCII), but rather URL encoding.
To decode the string in Java, use the URLDecoder class:
String result = java.net.URLDecoder.decode(encodedUrl, StandardCharsets.UTF_8.name());
In Java 10 , you can use this more concise syntax:
String result = java.net.URLDecoder.decode(encodedUrl, StandardCharsets.UTF_8);
Note:
URL encoding is used to make special characters, such as spaces and slashes, safe for transmission in a URL. It is important to decode the URL after transmission to prevent unexpected behavior or security vulnerabilities.
The above is the detailed content of How Do I Easily Decode URL-Encoded Strings in Java?. For more information, please follow other related articles on the PHP Chinese website!