How to do URL decoding in Java?
To decode a URL in Java, you can use the URLDecoder
class from the java.net
package.
The URLDecoder
class has a static decode
method that takes a string representing a URL-encoded value and a character encoding as arguments, and returns a decoded string.
Here's an example of how you can use the URLDecoder
class to decode a URL in Java:
import java.net.URLDecoder;
public class Main {
public static void main(String[] args) throws Exception {
String encodedUrl = "https://www.example.com/path%20with%20spaces";
String decodedUrl = URLDecoder.decode(encodedUrl, "UTF-8");
System.out.println(decodedUrl);
}
}
This code defines a string encodedUrl
that represents a URL-encoded value, and calls the decode
method of the URLDecoder
class to decode it. It passes the encodedUrl
string and the character encoding "UTF-8" as arguments to the decode
method, and prints the decoded string to the console.
I hope this helps! Let me know if you have any questions.