【发布时间】:2021-05-31 17:10:23
【问题描述】:
我有一个 java servlet,它读取 javascript 前端发送的参数。 javascript前端使用:
escape("{€ć") which becomes "%7B%u20AC%u0107"
Java servlet 就是这样做的:
private static final Pattern JAVASCRIPT_ESCAPE_SEQUENCE= Pattern.compile("%(u[0-9a-fA-F]{4}|[0-9a-fA-F]{2})");
static String unescape(String input) {
Matcher matcher = JAVASCRIPT_ESCAPE_SEQUENCE.matcher(input);
StringBuffer sb = new StringBuffer(input.length());
while(matcher.find()) {
String escapeSequence = matcher.group(1);
if (escapeSequence.startsWith("u")) {
escapeSequence = escapeSequence.substring(1);
}
char c = (char) Integer.parseInt(escapeSequence, 16);
//System.out.println(" converted " + Integer.parseInt("0107", 16));
matcher.appendReplacement(sb, Character.toString(c));
}
matcher.appendTail(sb);
return sb.toString();
}
String sDecodedContent = this.unescape(requestContent);
在 Java 中,变量 sDecodedContent 不是“{€ć”而是“{€?”并将它的字符串发送到后端,后端将不正确的字符串存储到数据库中。 为什么 ć 没有被正确解码? 问候
【问题讨论】:
-
尝试使用调试器并找出或检查
Character.toString(c);实际为您提供的值是您从注释行中获得的值。