简体   繁体   中英

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.

For instance, when creating a String object there is an overloaded constructor where you can specify character encoding:

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 Servlet,但是如果您这样做,可以尝试一下

request.setCharacterEncoding("UTF-8");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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