1
votes

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!

3

3 Answers

4
votes
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:

1
votes

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

1
votes

Icarin you mentioned:

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

Is this inside /conf/server.xml ?

Good day people!