senthil
senthil

Reputation: 11

UTF-8 encoding problem in unix machine

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

Upvotes: 1

Views: 4337

Answers (2)

Will Glass
Will Glass

Reputation: 4850

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");

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272267

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)

Upvotes: 1

Related Questions