Java 小程序中的 Unicode/日语字符
我正在编写一个小程序,它应该在 JLabel 上显示英语和日语(unicode)字符。当我在系统上运行小程序时,日语字符显示得很好,但当我从网页运行它时,我得到的只是 mojibake。如果日语字符硬编码到 HTML 中,则页面可以显示日语字符,但不能显示在小程序中。我很确定我以前见过这样的事情。我可以在 Java 代码中做些什么来解决这个问题吗?
I'm writing an applet that's supposed to show both English and Japanese (unicode) characters on a JLabel. The Japanese characters show up fine when I run the applet on my system, but all I get is mojibake when I run it from the web page. The page can display Japanese characters if they're hard-coded into the HTML, but not in the applet. I'm pretty sure I've seen this sort of thing working before. Is there anything I can do in the Java code to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的第一个猜测是 servlet 容器没有为您的 web 应用程序资源发回正确的字符集。查看 HTTP 嗅探器中的响应,看看包含哪些字符集 - 如果响应表明字符集是 CP-1252,则日语字符将无法正确解码。
您也许可以通过使用正确的字符集显式设置 Content-Type 标头来在代码中修复此问题;但我认为修复 servlet 容器的配置以返回相关资源的正确字符集更为合适。
My first guess would be that the servlet container is not sending back the right character set for your webapp resources. Have a look at the response in an HTTP sniffer to see what character set is included - if the response says that the charset is e.g. CP-1252, then Japanese characters would not be decoded correctly.
You may be able to fix this in code by explicitly setting a Content-Type header with the right charset; but I'd argue it's more appropriate to fix the servlet container's config to return the correct character set for the relevant resources.
好吧,我不确定是什么导致了这个问题,但我将所有内容都设置为以 UTF-8 读取和显示,现在似乎可以工作了。
Well I'm not sure what was causing the problem, but I set EVERYTHING to read in and display out in UTF-8 and it seems to work now.