Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

I am getting this exception while trying to generate a .PDF file from my application.

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

Here is the stack trace

java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "</"
    at java.net.URLDecoder.decode(Unknown Source)
     

Here is the code

StringBuffer outBuffer = new StringBuffer();
//some values are added to outBuffer .
String pdfXmlView = URLDecoder.decode(outBuffer.toString(), "utf-8");

While trying to decode using URLDecoder.decode() it is throwing that exception. I got the cause for the exception, it is coming because of % character in outBuffer.

If anyone knows how to solve this problem?

like image 740
subhashis Avatar asked May 20 '11 05:05

subhashis


3 Answers

There is a major issue with the accepted answer. Characters that get encoded have % and + signs in them, so although this helps with % and + characters in a string, it also doesn't decode things like %20 (space) because you are taking out the percent before decoding.

A solution is to replace %2B (+) and %25 (%) instead. Something like:

   public static String replacer(StringBuffer outBuffer) {
      String data = outBuffer.toString();
      try {
         data = data.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
         data = data.replaceAll("\\+", "%2B");
         data = URLDecoder.decode(data, "utf-8");
      } catch (Exception e) {
         e.printStackTrace();
      }
      return data;
   }

"+" is a special character which denotes a quantifier meaning one of more occurrences. So one should use "\+"

like image 67
fanfavorite Avatar answered Nov 20 '22 04:11

fanfavorite


I found the reason behind this exception. See this link for URLDecoder

So before calling URLDecoder.decode() i did this...

public static String replacer(StringBuffer outBuffer) {

    String data = outBuffer.toString();
    try {
        StringBuffer tempBuffer = new StringBuffer();
        int incrementor = 0;
        int dataLength = data.length();
        while (incrementor < dataLength) {
            char charecterAt = data.charAt(incrementor);
            if (charecterAt == '%') {
                tempBuffer.append("<percentage>");
            } else if (charecterAt == '+') {
                tempBuffer.append("<plus>");
            } else {
                tempBuffer.append(charecterAt);
            }
            incrementor++;
        }
        data = tempBuffer.toString();
        data = URLDecoder.decode(data, "utf-8");
        data = data.replaceAll("<percentage>", "%");
        data = data.replaceAll("<plus>", "+");
    } catch(Exception e) {
        e.printStackTrace();
    }
    return data;
}
like image 42
subhashis Avatar answered Nov 20 '22 06:11

subhashis


You can use this:

String stringEncoded = URLEncoder.encode(**YOUR_TEXT**, "UTF-8");

I had this problem when I used servlet (the darkness).

like image 1
Marcelo Rebouças Avatar answered Nov 20 '22 04:11

Marcelo Rebouças