Tuesday, 27 June 2017

How to decode url in java?

java.net.URLDecoder class provides 'decode' method to decode the encoded url.

public static String decode(String s, String encodingScheme)
Decodes the string 's' using given encodingScheme.

Every standard Java implementation must support below encoding schemes.

1.   US-ASCII
2.   ISO-8859-1
3.   UTF-8
4.   UTF-16BE
5.   UTF-16LE
6.   UTF-16

'java.nio.charset.Charset' class provides 'isSupported' method to check whether given character encoding is supported by your Java implementation or not.

Below program decodes an encoded url and print to the console.
package com.sample;

import java.io.UnsupportedEncodingException;

public class URLDecoder {

 public static void main(String args[]) throws UnsupportedEncodingException {

  String decodedURL = java.net.URLDecoder.decode("http%3A%2F%2Fself-learning-java-tutorial.blogspot.in%2F2014%2F09%2Fservlets.html", "UTF-8");
  System.out.println("decodedURL : " + decodedURL);
  
 }
}

Output
decodedURL : https://self-learning-java-tutorial.blogspot.com/2014/09/servlets.html


No comments:

Post a Comment