org.apache.commons.codec.net.URLCodec Example

Q

What is org.apache.commons.codec.net.URLCodec class? How to use org.apache.commons.codec.net.URLCodec class?

✍: FYIcenter.com

A

org.apache.commons.codec.net.URLCodec class is a Java class offered in commons-codec.jar that implements the 'www-form-urlencoded' encoding scheme, also misleadingly known as URL encoding.

This class is meant to be a replacement for standard Java classes URLEncoder and URLDecoder on older Java platforms, as these classes in Java versions below 1.4 rely on the platform's default charset encoding.

Two commonly used instance methods in org.apache.commons.codec.net.URLCodec class are:

1. The encoding method:

public String encode(String str) throws EncoderException

Encodes a string into its URL safe form using the default string charset. 
Unsafe characters are escaped.

Specified by:
    encode in interface StringEncoder
Parameters:
    str - string to convert to a URL safe form
Returns:
    URL safe string
Throws:
    EncoderException - Thrown if URL encoding is unsuccessful

2. The decoding method:

public String decode(String str) throws DecoderException

Decodes a URL safe string into its original form using the default 
string charset. Escaped characters are converted back to their original 
representation.

Specified by:
    decode in interface StringDecoder
Parameters:
    str - URL safe string to convert into its original form
Returns:
    original string
Throws:
    DecoderException - Thrown if URL decoding is unsuccessful

Here is a simple example of org.apache.commons.codec.net.URLCodec class:

// Copyright (c) 2016 FYIcenter.com
import org.apache.commons.codec.net.URLCodec;

// Example of using the URLCodec class
public class URLCodecExample {
   public static void main(String[] args) throws Exception {
      URLCodec codec = new URLCodec();

      System.out.println("encode() Example:");
      String inputString = "2_What Is activation.jar?.html";
      String outputString = codec.encode(inputString);
      String expectedString = "2_What+Is+activation.jar%3F.html";
      System.out.println("   Input string: "+inputString);
      System.out.println("   Encoded string: "+outputString);
      System.out.println("   Expected string: "+expectedString);

      System.out.println("decode() Example:");
      inputString = "2_What+Is+activation.jar%3F.html";
      outputString = codec.decode(inputString);
      expectedString = "2_What Is activation.jar?.html";
      System.out.println("   Input string: "+inputString);
      System.out.println("   Encoded string: "+outputString);
      System.out.println("   Expected string: "+expectedString);
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>\local\jdk-1.8.0\bin\javac 
   -cp C:\local\commons-codec-1.10\commons-codec-1.10.jar 
   URLCodecExample.java

C:\fyicenter>\local\jdk-1.8.0\bin\java
   -cp .;C:\local\commons-codec-1.10\commons-codec-1.10.jar 
   URLCodecExample

encode() Example:
   Input string: 2_What Is activation.jar?.html
   Encoded string: 2_What+Is+activation.jar%3F.html
   Expected string: 2_What+Is+activation.jar%3F.html
decode() Example:
   Input string: 2_What+Is+activation.jar%3F.html
   Encoded string: 2_What Is activation.jar?.html
   Expected string: 2_What Is activation.jar?.html

 

FAQ for Apache Commons Codec JAR Library

org.apache.commons.codec.digest.DigestUtils Example

Using commons-codec.jar in Java Programs

⇑⇑ FAQ for Apache Commons Codec JAR Library

2017-04-22, 1086👍, 0💬