Java URLDecoder:转义(%)模式中的非法十六进制字符-用于输入字符串:"</&引用;

Java URLDecoder:转义(%)模式中的非法十六进制字符-用于输入字符串:"</&引用;,java,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.

我在尝试从应用程序生成.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)
当尝试使用
urldecker.decode()
进行解码时,会引发该异常。我得到了异常的原因,它的出现是因为突发事件中的%字符

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


谢谢。

我找到了这个异常背后的原因

所以在调用
urldecker.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;
}
publicstaticstringreplacer(stringbufferextuffer){
字符串数据=exputffer.toString();
试一试{
StringBuffer tempBuffer=新的StringBuffer();
int incrementor=0;
int dataLength=data.length();
while(增量<数据长度){
字符数=data.charAt(递增);
如果(字符数=='%'){
tempBuffer.append(“”);
}else if(字符数=='+'){
tempBuffer.append(“”);
}否则{
tempBuffer.append(字符数);
}
Encrementor++;
}
data=tempBuffer.toString();
数据=URLDecover.decode(数据,“utf-8”);
data=data.replaceAll(“,%”);
data=data.replaceAll(“,”+”);
}捕获(例外e){
e、 printStackTrace();
}
返回数据;
}

公认的答案存在一个重大问题。编码后的字符中有%和+符号,因此尽管这有助于在字符串中使用%和+字符,但它也不会解码%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;
   }

“+”是一个特殊字符,表示一个量词,表示一个或多个事件。因此,应该使用“\+”

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

如果您只面临**%**的问题。那么这将有助于:
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;
}
受保护的静态字符串编码器(字符串localTopic1){ 字符串localTopic=localTopic1; 试一试{ StringBuffer tempBuffer=新的StringBuffer(); int incrementor=0; int dataLength=localTopic.length(); while(增量<数据长度){ characterAt=localTopic.charAt(增量); int next_char_index=incrementor+1; int第三个索引=下一个字符索引+1; 字符charAt_nextIndex=''; char charAt_thirdIndex=''; 字符串stringAt_nextIndex=“”; if(下一个字符索引<数据长度){ charAt\u nextIndex=localTopic.charAt(下一个char\u索引); stringAt_nextIndex=charAt_nextIndex.toString(); } if(第三个索引<数据长度) charAt_thirdIndex=localTopic.charAt(第三个索引); 如果(characterAt=='%'){ if(stringAt_nextIndex.matches(“[A-F2-9]”){ 如果(字符第三个索引=“”| |字符第三个索引=“”%){ tempBuffer.append(“”); } 否则{ tempBuffer.append(characterAt); } } 否则{ tempBuffer.append(“”); } }否则{ tempBuffer.append(characterAt); } Encrementor++; } localTopic=tempBuffer.toString(); }捕获(例外e){ e、 printStackTrace(); } 返回localTopic; }
您可以使用以下功能:

String stringEncoded = URLEncoder.encode(**YOUR_TEXT**, "UTF-8");
我在使用servlet(黑暗)时遇到了这个问题


)()

在通过网络传输数据时,我也面临同样的问题。
提供示例代码,该代码引发异常
urldecker:escape(%)中的非法十六进制字符

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

var encodedData=encodeURIComponent('Yash%');
var xmlhttp=new XMLHttpRequest();
open(“POST”https://yash.ssl.com:8443/ServletApp/test“,对);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.send(“data=“+encodedData”);
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
console.log(“数据上传成功::”;
}
};
接受POST请求的Sevlet代码

protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
试一试{
System.out.println(“================/test |”+request.getParameter(“数据”);
字符串networkData=urldecker.decode(request.getParameter(“数据”),“UTF-8”);
System.out.println(“Ajax调用数据:+networkData”);
}捕获(例外情况除外){
例如printStackTrace();
}
}
正如@
Marcelo RebouçAs
在解码字符串之前建议的那样,我正在对其进行编码。
Java类«
允许使用字符“%”,但被解释为特殊转义序列的开头。

Javascript函数
encodeURI()和encodeURIComponent()

//URL解码器:转义(%)模式中的非法十六进制字符
String stringEncoded=urlcoder.encode(request.getParameter(“数据”),“UTF-8”);
字符串networkData=urldecker.decode(stringEncoded,“UTF-8”);

输入字符串是什么?您是如何尝试生成PDF的?堆栈跟踪是什么样子的?为什么要回滚上面的框架文本?这是一个主要问题。编码后的字符串中有%和+符号,因此尽管这有助于处理字符串中的这些字符
String stringEncoded = URLEncoder.encode(**YOUR_TEXT**, "UTF-8");