简体   繁体   English

日语字符在Java中无法正确显示

[英]Japanese character not displayed properly in Java

I have text box and a button. 我有文本框和一个按钮。 I am entering Japanese characters like こんにちは in text box. 我在文本框中输入日语字符,例如こんにちは。

When I click on button email is sent with the text in text box. 当我单击按钮时,将在文本框中发送带有文本的电子邮件。

But when email receive it displays some junk character instead Japanese characters. 但是,当收到电子邮件时,它将显示一些垃圾字符而不是日语字符。

Can anybody please tell me why this is happening? 有人可以告诉我为什么会这样吗?

Thanks in advance. 提前致谢。

You should make sure that you are using a character set that supports Japanese characters, like Unicode. 您应该确保使用的字符集支持日语字符,例如Unicode。

For instance, when creating a String object there is an overloaded constructor where you can specify character encoding: 例如,当创建一个String对象时,有一个重载的构造函数,您可以在其中指定字符编码:

byte[] utf8Characters = { /* UTF-8 encoded characters */ };
String s = new String(characters, "UTF-8"); // Decode bytes using UTF-8.

Also when converting Strings to bytes (ie when streaming data) you can use: 另外,在将字符串转换为字节时(即在流数据传输时),您可以使用:

byte[] utf8EncodedBytes = s.getBytes("UTF-8"); // Encode to UTF-8.

If you do not specify character encoding it will default to some charset which might not support the characters you need. 如果不指定字符编码,它将默认为某些字符集,可能不支持所需的字符。

Java Doc says: "The default charset is determined during virtual-machine startup and typically depends upon the locale and charset being used by the underlying operating system." Java Doc说:“默认字符集是在虚拟机启动期间确定的,通常取决于底层操作系统使用的语言环境和字符集。”

我不是很确定您是否正在使用Java Servlet,但是如果您这样做,可以尝试一下

request.setCharacterEncoding("UTF-8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM