I have a mule flow with a spring bean that reads a static XML file encoded in utf-8. However it messes up all the non-english characters. The bean is defined as follows:
<spring:bean id="LoadFile" name="Bean" class="java.lang.String">
<spring:constructor-arg>
<spring:bean id="Test" name="org.springframework.util.FileCopyUtils" class="org.springframework.util.FileCopyUtils" factory-method="copyToByteArray">
<spring:constructor-arg type="java.io.InputStream" value="classpath:Settings.xml"/>
</spring:bean>
</spring:constructor-arg>
</spring:bean>
</spring:beans>
If I read the same file using my own code like :
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream(name);
It's utf-8 and works. How can I define the springbean to care about encodings?
Regards
Add UTF-8 charset constructor arg for String bean
<spring:bean id="LoadFile" name="Bean" class="java.lang.String">
<spring:constructor-arg index="0">
<spring:bean id="Test" name="org.springframework.util.FileCopyUtils" class="org.springframework.util.FileCopyUtils" factory-method="copyToByteArray">
<spring:constructor-arg type="java.io.InputStream" value="classpath:Settings.xml"/>
</spring:bean>
</spring:constructor-arg>
<spring:constructor-arg index="1" value="UTF-8">
</spring:bean>
Java equivalent
new String(byte_array, "UTF-8")
This is because you are creating a String
by calling its constructor that takes a byte[]
, which will interpret the bytes using the default character encoding of your system, which is probably not UTF-8.
Specify the character encoding, by using the right constructor of String
. This should work (I haven't tested it):
<spring:bean id="LoadFile" name="Bean" class="java.lang.String">
<spring:constructor-arg>
<spring:bean id="Test" name="org.springframework.util.FileCopyUtils" class="org.springframework.util.FileCopyUtils" factory-method="copyToByteArray">
<spring:constructor-arg type="java.io.InputStream" value="classpath:Settings.xml"/>
</spring:bean>
</spring:constructor-arg>
<spring:constructor-arg value="UTF-8"/>
</spring:bean>
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.