java Websphere 8.5.5 UTF-8 编码问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38399270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Websphere 8.5.5 UTF-8 encoding issue
提问by amicoderozer
I have a problem with my application on Websphere 8.5.5. It's a spring rest application that is used to send email. In my service I receive the mail details and I use spring to forward it to the receivers. It works fine but when I try to send messages with non ascii character the message arrives with the special characters replaced by a question mark. The UTF-8encoding doesn't work. In my pc the application runs on Liberty Profile server and initially it doesn't work either. I googled the problem and found that spring needs a filter to interpret the special characters, so I added this lines to my web.xml
我在Websphere 8.5.5上的应用程序有问题。这是一个用于发送电子邮件的弹簧休息应用程序。在我的服务中,我收到邮件详细信息,并使用 spring 将其转发给接收者。它工作正常,但是当我尝试使用非 ascii 字符发送消息时,消息到达时将特殊字符替换为问号。在UTF-8编码不起作用。在我的电脑中,应用程序在 Liberty Profile 服务器上运行,最初它也不起作用。我用谷歌搜索了这个问题,发现 spring 需要一个过滤器来解释特殊字符,所以我在我的web.xml 中添加了这一行
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
On Liberty works fine but when I deploy the app on Websphere it doesn't.
在 Liberty 上工作正常,但是当我在 Websphere 上部署应用程序时却没有。
I tried to directly translate the message in utf-8 with the String constructor:
我尝试使用 String 构造函数直接翻译 utf-8 中的消息:
new String(byteArray, "UTF-8")
and the same thing happens. It works on liberty but it doesn't on Websphere. Then I found that you can force WebSphere to use UTF-8by adding these JVM Arguments:
同样的事情发生了。它适用于自由,但不适用于 Websphere。然后我发现您可以通过添加这些 JVM 参数来强制 WebSphere 使用UTF-8:
-Dclient.encoding.override=UTF-8
-Dfile.encoding=UTF-8
like explained in this answer: Character encoding issues on websphere
就像在这个答案中解释的那样: Websphere 上的字符编码问题
I set the parameters on my server and restart like explained in many guides I found on the internet, but obviously it didn't work.
我在我的服务器上设置了参数并按照我在互联网上找到的许多指南中的解释重新启动,但显然它不起作用。
This is the output of locale command in linux machine where WAS run:
这是运行 WAS 的 linux 机器中 locale 命令的输出:
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
I'am stuck and do not know what else to try.
我被卡住了,不知道还能尝试什么。
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Haxiel
The encoding.properties
file located at <PROFILE_ROOT>/properties
is one more location that contains encoding settings. The default value (for English) is en=ISO-8859-1
. Update this to en=UTF-8
and restart the server.
encoding.properties
位于的文件<PROFILE_ROOT>/properties
是另一个包含编码设置的位置。默认值(英语)是en=ISO-8859-1
. 将此更新到en=UTF-8
并重新启动服务器。