其他分享
首页 > 其他分享> > 用于特殊字符的Spring resourcebundle的UTF-8编码问题

用于特殊字符的Spring resourcebundle的UTF-8编码问题

作者:互联网

我正在使用Spring ResourceBundle从我的.properties文件中检索消息包.它会像以下一样保留特殊的欧洲字符:

Ü, ß. ä, ö, ü

MessageSouce bean如下(我确保遵循UTF-8编码)

    <bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:/META-INF/i18/messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

当我尝试在我的Java代码中检索消息时,我正在获取垃圾字符.

如果我使用下面的代码,它有助于识别几个字符,但休息仍然显示为??

 return new String (bundleString.getBytes(), "UTF-8")

然后我用下面来编码我的属性文件,但仍然没有效果

native2ascii -encoding utf8 resources.utf8 resources.properties

我还尝试在记事本中手动打开我的属性文件并设置UTF-8编码,但没有效果.
我看到一个here的帖子,与我的问题完全相同.但该解决方案使用PropertiesBundle,而我只能使用基于Spring的解决方案.然而,即使是那个链接中接受的答案对我来说也不起作用,并且给了垃圾字符.

请建议任何可能的解决方案

解决方法:

我遇到了同样的问题,@ sunny_dev的答案对我有用.
我不明白为什么没有这个问题的答案,所以我正在更新这个问题.

@sunny_dev回答:

Karol, I solved the problem by opening and saving the .properties file
in TextPad as “UTF-8” encoding and “Unix” platform. I had taken that
same approach, however in Notepad++ without the positive outcome
earlier. – sunny_dev

再次感谢@sunny_dev

标签:encoding,spring,utf-8,iso-8859-1
来源: https://codeday.me/bug/20190708/1403308.html