Home >Java >javaTutorial >How Do I Easily Decode URL-Encoded Strings in Java?

How Do I Easily Decode URL-Encoded Strings in Java?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-11 06:40:13521browse

How Do I Easily Decode URL-Encoded Strings in Java?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn