views:

66

answers:

2

I am sending a response using the following code:

response.setHeader("Content-Encoding","UTF-8"); response.setContentType("text/plain charset=UTF-8");

PrintWriter outWriter = response.getWriter(); String returnString = new String(dataController.translateFile(documentBuffer).getBytes(), "UTF-8"); outWriter.print(returnString);

When I run my web app using tomcat 6.0.29 on Eclipse, the resulting txt file gives me a txt file with utf-8 encoding (I can see a lot of arabic or chinese symbols without problem), however once I deploy the WAR file of the project, the resulting txt file is filled with question marks instead of chinese or arabic characters.

Any idea of what might be the problem?

Also I added URIEncoding="UTF-8" on each Connector tag in the server.xml file from CONF/ of Tomcat. The same tomcat Eclipse is using but to no avail.

Thanks in advance!

+2  A: 

.

response.setHeader("Content-Encoding","UTF-8");

This is superfluous when the content type is been set.

response.setContentType("text/plain charset=UTF-8");

This is in fact wrong. A semicolon is missing before charset.

Also I added URIEncoding="UTF-8" on each Connector tag in the server.xml file from CONF/ of Tomcat.

This has only effect on GET request parameters.


What you need is the following:

response.setCharacterEncoding("UTF-8"); // Otherwise platform default encoding will be used to write the characters.
response.setContentType("text/plain; charset=UTF-8");

Call this before calling response.getWriter() or getOutputStream().

See also:

BalusC
I did the modifications you suggested and the program did fine on Eclipse, however directly on windows there was no change. I have found the answer (see the answer to this question for more details).
Icarin
A: 

I have found the answer, even though I had some errors and some unnecessary code which where pointed out by BalusC, the solution was to create an environment variable on windows (or at the apache level I guess), then restart the server.

This variable was: JAVA_OPTS with the value: -Dfile.encoding=UTF-8

Icarin
This is a Sun/Oracle JVM specific setting to set the encoding to read files (which thus ain't going to work in other JVM's). In other words, the `data Controller.translateFile(documentBuffer)` is possibly not using UTF-8 to process the text file.
BalusC