URLDecoder:转义(%)模式中的非法hex字符 – 对于输入字符串:“</”

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

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()进行解码时,它会抛出exception。 我得到了exception的原因,它是因为outBuffer中的%字符而来的。

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

谢谢。

接受的答案存在一个主要问题。 被编码的字符中包含%和+符号,因此虽然这有助于字符串中的%和+字符,但它也不会解码%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; } 

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

我找到了这个例外背后的原因。 请参阅此链接以获取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(""); } else if (charecterAt == '+') { tempBuffer.append(""); } else { tempBuffer.append(charecterAt); } incrementor++; } data = tempBuffer.toString(); data = URLDecoder.decode(data, "utf-8"); data = data.replaceAll("", "%"); data = data.replaceAll("", "+"); } catch(Exception e) { e.printStackTrace(); } return data; } 

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

 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(""); } else{ tempBuffer.append(characterAt); } } else{ tempBuffer.append(""); } }else { tempBuffer.append(characterAt); } incrementor++; } localTopic = tempBuffer.toString(); } catch (Exception e) { e.printStackTrace(); } return localTopic; } 

你可以用这个:

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

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

;)