用java做了一个项目,配置文件本来都读取没问题的,但是我把properties文件的编码改成了utf8,然后程序读取的都是乱码,总共有两个地方使用了:
1.使用java代码读取:
Properties prop=new Properties();
prop.load(Client.class.getClassLoader().getResourceAsStream("config.properties"));
改成:
Properties prop=new Properties();
prop.load(new InputStreamReader(Client.class.getClassLoader().getResourceAsStream("config.properties"), "UTF-8"));
2.spring中使用
<util:properties id="properties" location="classpath:all.properties"/>
改成了
<beans:bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <beans:property name="location" value="classpath:all.properties"/> <beans:property name="fileEncoding" value="UTF-8"/> </beans:bean>
于是乱码问题解决了,感谢网络,这么快速的解决了
这个问题的根源应该是java的编码或者tomcat的编码是ASCII的。应该是。。。
参考资料:
http://pig345.iteye.com/blog/725974
http://stackoverflow.com/questions/8662284/spring-utilproperties-can-you-change-the-encoding-to-utf-8