Tomcat 未正确编码包含 unicode 字符的字符串文字。问题出现在 Linux 服务器上,但不出现在我的开发机器 (Windows) 上。它仅影响字符串文字(不影响从数据库或文件中读取的字符串!!!)。
- 我已经在连接器标签 (server.xml) 中设置了
URIEncoding="utf-8"
。 - 我使用了 setCharacterEncoding()。
- 我检查了堆栈跟踪(没有可能设置编码的过滤器)。
- 我已经设置了 LANG 环境变量
- 我检查了 HTTP header ,它们是正确的 (Content-Type=text/plain;charset=utf-8)
- 我检查了浏览器的编码,它是正确的 (UTF-8)
以上均无效。关于我可能遗漏的任何想法?
public class Test extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/plain;");
Writer w = resp.getWriter();
w.write("Μαλακία Latin"); //Some unicode characters
w.close();
}
上面在浏览器中显示了这一点。 γγγ»γδεγε拉丁语
最佳答案
您可以在编译时传入-encoding 'utf-8' 或-encoding 'iso-8859-1' 来在javac 读取文件时强制对文件进行编码。只要确保它与您的 .java 文件实际编码的任何编码相匹配即可。
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html
-encoding encoding Set the source file encoding name, such as EUC-JP and UTF-8. If -encoding is not specified, the platform default converter is used.
关于java - 如何在tomcat中使用UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9822663/