URLDecoder: Illegal hex characters in escape (%) pattern - For input string: “</”
我在尝试从应用程序生成.pdf文件时遇到此异常。
1 |
这是堆栈跟踪
1 2 | java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string:"</" at java.net.URLDecoder.decode(Unknown Source) |
这是密码
1 2 3 | StringBuffer outBuffer = new StringBuffer(); //some values are added to outBuffer . String pdfXmlView = URLDecoder.decode(outBuffer.toString(),"utf-8"); |
当试图使用
如果有人知道如何解决这个问题,请帮助我。
谢谢。
接受的答案有一个重大问题。被编码的字符中有%和+符号,因此尽管这对字符串中的%和+字符有帮助,但它也不会解码像%20(空格)这样的东西,因为在解码之前要去掉百分比。
解决方案是替换%2b(+)和%25(%)。类似:
1 2 3 4 5 6 7 8 9 10 11 | 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; } |
"+"是一个特殊的字符,它表示一个量词,表示多个出现中的一个。所以应该使用"+"
我发现了这个例外背后的原因。请参阅urldecoder的此链接
所以在打电话给
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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; } |
我在通过网络传输数据时也面临同样的问题。< BR>提供示例代码,引发异常
传递字符串
1 2 3 4 5 6 7 8 9 10 11 12 | 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代码。
1 2 3 4 5 6 7 8 9 10 | 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(); } } |
正如@EDOCX1[3]在解码字符串之前建议的那样,我正在对它进行编码。< BR>Java EDCOX1?4类?
1 2 3 | // URLDecoder: Illegal hex characters in escape (%) pattern String stringEncoded = URLEncoder.encode( request.getParameter("data"),"UTF-8"); String networkData = URLDecoder.decode( stringEncoded,"UTF-8"); |
您可以使用:
1 |
我使用servlet(黑暗)时遇到了这个问题。
;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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; } |
请检查您输入到解码器,已传递到解码器方法的OutBuffer应该是一个编码值,这样就不会发生此问题。