开发者

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

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.IllegalArgumentExceptio开发者_如何学JAVAn: 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?


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 "\+"


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;
}


You can use this:

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

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


If you are facing issue only with **%**. Then this would help:

   protected static String encoder(String localTopic1){
        String localTopic =localTopic1;
        try {
            StringBuffer tempBuffer = new StringBuffer();
            int incrementor = 0;
            int dataLength = localTopic.length();
            while (incrementor < dataLength) {
            char characterAt = localTopic.charAt(incrementor);
            int next_char_index = incrementor+1;
            int third_index = next_char_index+1;
            Character charAt_nextIndex = ' ';
            char charAt_thirdIndex = ' ';
            String stringAt_nextIndex = "";

            if(next_char_index < dataLength){
                    charAt_nextIndex = localTopic.charAt(next_char_index);
                    stringAt_nextIndex = charAt_nextIndex.toString();
            }
            if(third_index < dataLength)
                    charAt_thirdIndex = localTopic.charAt(third_index);


            if (characterAt == '%') {
                    if(stringAt_nextIndex.matches("[A-F2-9]")){

                            if(charAt_thirdIndex == ' ' || charAt_thirdIndex == '%'){
                                    tempBuffer.append("<percentage>");
                            }
                            else{
                                    tempBuffer.append(characterAt);
                            }
                    }
                    else{
                            tempBuffer.append("<percentage>");
                    }

            }else {
                    tempBuffer.append(characterAt);
            }
            incrementor++;
    }
    localTopic = tempBuffer.toString();
} catch (Exception e) {
    e.printStackTrace();
}
return localTopic;
}


I have face the same problem, while transfering the data over network.
Providing the sample code, which throws the Exception URLDecoder: Illegal hex characters in escape (%)

Sample Ajax Code which passes the string 'Yash %':

var encodedData = encodeURIComponent('Yash %');

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "https://yash.ssl.com:8443/ServletApp/test", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("data="+encodedData);

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            console.log("data uploaded successfully :: ");
        }
    };

Sevlet code which accepts the POST request.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        System.out.println(" ===== ------ ===== /test | " + request.getParameter("data"));

        String networkData = URLDecoder.decode( request.getParameter("data"), "UTF-8");
        System.out.println("Ajax call data : "+ networkData);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

As @Marcelo Rebouças suggested before decoding a string, I am encoding it.
Java URLDecoder class « The character "%" is allowed but is interpreted as the start of a special escaped sequence.
Javascript URL functions encodeURI() and encodeURIComponent()

// URLDecoder: Illegal hex characters in escape (%) pattern
String stringEncoded = URLEncoder.encode( request.getParameter("data"), "UTF-8");
String networkData = URLDecoder.decode( stringEncoded, "UTF-8");


Please check your input to decoder, the outbuffer which has been passed to decoder method should be an encoded value, then this issue will not occur.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜