views:

50

answers:

2

Im exporting a set of data to excel in java, the data has certain non ascii characters, while exporting in Windows machine the data are coming correctly in UTF-8 encoded format.But when i deploy my code in Unix machine it is not working properly.UTF-8 encoding is not working properly. Im using Tomcat 5.5 server.I have also included URIencoding="UTF_8" parameter in server.xml. But still in unix machine it is not working properly

+1  A: 

Running Tomcat with a

-Dfile.encoding=UTF8

option will force the VM to adopt UTF8 as it's default encoding regardless of your environment. I suspect that's your problem (and it's good practise nonetheless)

Brian Agnew
I tried -Dfile.encoding=UTF8 option in CATALINA_OPTS in tomcat start shell script. But still its not working.Please help me in this.
senthil
It is coming fine in UI but when i export the data only it is not coming in UTF format in UNIX. please help me
senthil
A: 

When you are working with UTF-8 data it can be very fragile. Every step in the chain needs to specify utf8.

(1) In the database, be sure the table has UTF8 encoding and collation. "show table status" will tell you this. Google for "mysql alter table utf8" for more info.

(2) The JDBC connection needs to specify the UTF8 encoding. For Connector/J this will be something similar to:

useUnicode=true&characterEncoding=UTF-8

(3) Every stream read/write, every string to byte conversion in the code needs to specify UTF-8 as the encoding. It's easy to miss one. If it's missed Java will use the system default which will vary server to server.

(4) Not applicable here, but for user submitted data from a form, you need to set the request character encoding. (I do this in a servlet filter)

request.setCharacterEncoding("UTF-8");

(5) Probably not applicable here, but if you output HTML/XML/text from your servlet, set the HTTP character encoding header. (This might apply if you are generating the Excel file as an XML file).

response.setContentType("text/html; charset=UTF-8");
Will Glass