本文介绍了URLDecoder:转义(%)模式中的非法十六进制字符 - 对于输入字符串:"< / lt; /"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试从我的应用程序生成.PDF文件时遇到此异常。
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:....
这是堆栈跟踪
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中的%字符而来的。
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, please help me.
谢谢。
推荐答案
我找到了这个例外背后的原因。
I found the reason behind this exception. See this link for URLDecoder
所以在调用之前,URLDecoder.decode()
我这样做了......
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;
}
这篇关于URLDecoder:转义(%)模式中的非法十六进制字符 - 对于输入字符串:"< / lt; /"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!