Decode a URL

In this chapter you will learn:

  1. Decode a URL with URLDecoder
  2. Decode a URL with parameters

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:

  1. What is URI class
Home » Java Tutorial » URL URI
URL
URL Creation
URL for jar file
URL Components
Convert file path to URL
URL Relative
URL Protocol
Read from URL
Compare URL
URLConnection
HTTP Header
URLConnection Post
Cookie
URLConnection Read
HttpURLConnection
HttpURLConnection Properties
HttpURLConnection proxy
HttpURLConnection Authenticator
HTTPS
JarURLConnection
Encode a URL
Decode a URL
URI
URI Normalization
URI Resolution
URI Relativization
Convert URI to URL
IP Address
IP Ping
NetworkInterface