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
Release: 2024-12-11 06:40:13
Original
467 people have browsed it

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
Copy after login

To a decoded form like this:

https://mywebsite/docs/english/site/mybook.do&request_type
Copy after login

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());
Copy after login

In Java 10 , you can use this more concise syntax:

String result = java.net.URLDecoder.decode(encodedUrl, StandardCharsets.UTF_8);
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template