Wednesday, November 11, 2015

Java - Solution:URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " %"

Problem: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " %"

Solution: Just replace the special  characters " % " with %" 25 " before call URLDecoder.decode()

Class: DecorerDemo

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

public class DecorerDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "*Hello special characters %%%%%";
        try {
           
            str = str.replaceAll("%", "%25");
            System.out.println(URLDecoder.decode(str, "UTF-8").replaceAll("\n", "<br />"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}


Result: *Hello special characters %%%%%

No comments:

Post a Comment