Decode a URL
In this chapter you will learn:
Decode a URL
URLDecoder
declares the following class
method for decoding an encoded string:
String decode(String s, String enc)
import java.net.URLDecoder;
/* j a va 2s .c o m*/
public class MainClass {
public static void main(String[] args) throws Exception {
String url = "http://www.%20test.com/";
String decoded = URLDecoder.decode(url, "UTF-8");
System.out.println(decoded);
}
}
The code above generates the following result.
Decode a URL with parameters
import java.net.URLDecoder;
//from j a va2s . c o m
public class MainClass {
public static void main(String[] args) {
String input = "http://www.java2s.com/query?pg=q&kl=XX&stype=stext&q=%2B%22Java+Programming%22&search.x=30&search.y=7";
System.out.println(input);
try {
String output = URLDecoder.decode(input, "UTF8");
System.out.println(output);
} catch (Exception e) {
System.err.println("Malformed URL");
}
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » URL URI