URLDecoder:转义(%)模式中的非法十六进制字符 - 对于输入字符串:“<! - ” - >

时间:2011-05-20 05:04:12

标签: java

我在尝试从我的应用程序生成.PDF文件时遇到此异常。

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

这是堆栈跟踪

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

这是代码

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

尝试使用URLDecoder.decode()解码时,它会抛出异常。我得到了异常的原因,它是因为outBuffer中的%字符而来的。

如果有人知道如何解决这个问题,请帮助我。

感谢。

6 个答案:

答案 0 :(得分:43)

接受的答案存在一个主要问题。被编码的字符中包含%和+符号,因此虽然这有助于字符串中的%和+字符,但它也不会解码%20(空格)之类的内容,因为您在解码之前取出百分比。

解决方案是替换%2B(+)和%25(%)。类似的东西:

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

“+”是一个特殊字符,表示一个更多出现次数的量词。所以应该使用“\ +”

答案 1 :(得分:5)

我找到了这个例外背后的原因。 See this link for URLDecoder

所以在致电URLDecoder.decode()之前我做了这个......

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

答案 2 :(得分:0)

请检查你对解码器的输入,已经传递给解码器方法的outbuffer应该是一个编码值,那么这个问题就不会发生了。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

您可以使用此:

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

当我使用servlet(黑暗)时遇到了这个问题。

;)

答案 5 :(得分:0)

在通过网络传输数据时,我遇到了同样的问题。
提供示例代码,该代码将引发异常URLDecoder: Illegal hex characters in escape (%)

传递字符串'Yash %'的示例Ajax代码:

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 :: ");
        }
    };

接受POST请求的Sevlet代码。

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

正如@ Marcelo Rebouças所建议,在解码字符串之前,我正在对其进行编码。
Java URLDecoder类«The character "%" is allowed but is interpreted as the start of a special escaped sequence.
JavaScript URL函数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");
相关问题