我的服务器无法通过 POST 请求接收韩文字符,也无法将它们写入控制台。
这是我的代码:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
StringBuffer buffer = new StringBuffer();
String line = null;
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
buffer.append(line);
String requestMessage = buffer.toString();
//working
System.out.println("teniszütő");
//not working (???????)
System.out.println("\uc548\ub155\ud558\uc138\uc694\uc138\uacc4");
//not working (??)
System.out.println("여니");
//accents are working, Korean characters aren't
System.out.println(requestMessage);
//writing 3F (the code of the ? instead of the code of the character)
System.out.println(bytesToHexString(requestMessage.getBytes()));
out.println("Answer");
}
我使用 Eclipse,如果我检查项目的属性,我可以看到“文本文件编码”是“从容器 (UTF-8) 继承”。如果我制作一个普通的 HelloWorld 应用程序(具有相同的设置),它会打印韩文字符。
谢谢你的帮助!
最佳答案
我找到了解决方案。我必须在我的 <Connector>
中设置 URIEncoding="UTF-8"在 server.xml 中是这样的:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
关于java - 从 Tomcat Servlet 中读取韩文字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26878764/