我有一个Java servlet,它通过HTTP GET请求从上游系统接收数据。该请求包含一个名为text的参数。如果上游系统将此参数设置为:
TEST3 please ignore:
它在上游系统的日志中显示为:
00 54 00 45 00 53 00 54 00 33 00 20 00 70 00 6c //TEST3 pl
00 65 00 61 00 73 00 65 00 20 00 69 00 67 00 6e //ease ign
00 6f 00 72 00 65 00 3a //ore:
( //注释实际上不会出现在日志中)
在我的servlet中,我使用以下命令读取此参数:
String text = request.getParameter("text");
如果我将text
的值打印到控制台,它会显示为:
T E S T 3 p l e a s e i g n o r e :
如果在调试器中检查text
的值,它看起来如下所示:
\u000T\u000E\u000S\u000T\u0003\u0000 \u000p\u000l\u000e\u000a\u000s\u000e\u0000
\u000i\u000g\u000n\u000o\u000r\u000e\u000:
所以看起来字符编码有问题。上游系统应该使用UTF-16。我的猜测是servlet采用UTF-8,因此读取的字符数是应有字符数的两倍。对于消息"TEST3 the:“,每个字符的第一个字节为00
。这在被servlet读取时被解释为一个空格,它解释了当servlet记录消息时出现在每个字符之前的空格。
显然,我的目标只是在阅读text
请求参数时收到消息"TEST3请忽略:“。我猜我可以通过指定请求参数的字符编码来实现这一点,但我不知道如何做到这一点。
发布于 2014-01-24 20:02:51
像这样使用
new String(req.getParameter("<my request value>").getBytes("ISO-8859-1"),"UTF-8")
发布于 2012-06-19 19:57:15
尝试对此使用筛选器
public class CustomCharacterEncodingFilter implements Filter {
public void init(FilterConfig config) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
public void destroy() {
}
这应该为整个应用程序设置正确的编码
发布于 2012-06-19 19:49:32
看起来它是用UTF-16LE
(Little Endian)编码的,下面是一个成功打印字符串的类:
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
public class Test {
public static void main(String[] args) throws UnsupportedEncodingException {
String hex = "00 54 00 45 00 53 00 54 00 33 00 20 00 70 00 6c" +
"00 65 00 61 00 73 00 65 00 20 00 69 00 67 00 6e" +
"00 6f 00 72 00 65 00 3a"; // + " 00";
System.out.println(new String(new BigInteger(hex.replaceAll(" ", ""), 16).toByteArray(), "UTF-16LE"));
}
}
输出:
TEST3 please ignore?
输入中添加了两个零的输出
TEST3 please ignore:
更新
要让它与您的Servlet
一起工作,您可以尝试:
String value = request.getParameter("text");
try {
value = new String(value.getBytes(), "UTF-16LE");
} catch(java.io.UnsupportedEncodingException ex) {}
更新
参见下面的link,它验证生成的十六进制实际上是UTF-16LE
https://stackoverflow.com/questions/11100107
复制相似问题