Java URL encode and decode

Description

Java URL encode and decode

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Main {
  public static void main(String[] args) {
    String source = "this is a test for 2.5% and &";
    String encoded;//from   w  w  w . ja va  2  s .  c o  m
    try {
      encoded = URLEncoder.encode(source, "utf-8");
      String decoded = URLDecoder.decode(encoded, "utf-8");
      System.out.println("Source: " + source);
      System.out.println("Encoded: " + encoded);
      System.out.println("Decoded: " + decoded);      
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

  }
}



PreviousNext

Related