Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Tomcat服务器文件下载编码有问题_Java_Servlets_Utf 8_Character Encoding - Fatal编程技术网

Java Tomcat服务器文件下载编码有问题

Java Tomcat服务器文件下载编码有问题,java,servlets,utf-8,character-encoding,Java,Servlets,Utf 8,Character Encoding,我使用以下代码发送响应: 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(),

我使用以下代码发送响应:

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);
当我在Eclipse上使用tomcat 6.0.29运行我的web应用程序时,生成的txt文件会给我一个utf-8编码的txt文件(我可以看到很多阿拉伯文或中文符号,没有问题),但是一旦我部署了项目的WAR文件,生成的txt文件就会充满问号,而不是中文或阿拉伯文字符

你知道有什么问题吗

此外,我还在Tomcat的CONF/中server.xml文件中的每个连接器标记上添加了
URIEncoding=“UTF-8”
。同一个tomcat Eclipse正在使用,但没有用

提前谢谢

response.setHeader("Content-Encoding","UTF-8");
设置内容类型时,这是多余的

response.setContentType("text/plain charset=UTF-8");
这其实是错误的。
字符集
前面缺少分号

此外,我还在Tomcat的CONF/中的server.xml文件中的每个连接器标记上添加了URIEncoding=“UTF-8”

这仅对GET请求参数有效


您需要的是以下内容:

response.setCharacterEncoding("UTF-8"); // Otherwise platform default encoding will be used to write the characters.
response.setContentType("text/plain; charset=UTF-8");
在调用
response.getWriter()
getOutputStream()
之前调用此函数

另见:
设置内容类型时,这是多余的

response.setContentType("text/plain charset=UTF-8");
这其实是错误的。
字符集
前面缺少分号

此外,我还在Tomcat的CONF/中的server.xml文件中的每个连接器标记上添加了URIEncoding=“UTF-8”

这仅对GET请求参数有效


您需要的是以下内容:

response.setCharacterEncoding("UTF-8"); // Otherwise platform default encoding will be used to write the characters.
response.setContentType("text/plain; charset=UTF-8");
在调用
response.getWriter()
getOutputStream()
之前调用此函数

另见:

我找到了答案,尽管我遇到了一些错误和一些不必要的代码,解决方案是在windows上创建一个环境变量(或者我想是在apache级别),然后重新启动服务器


这个变量是:JAVA\u OPTS,值为:-Dfile.encoding=UTF-8

我已经找到了答案,尽管我遇到了一些错误和一些不必要的代码,解决方案是在windows上创建一个环境变量(或者我想是在apache级别),然后重新启动服务器

这个变量是:JAVA\u OPTS,其值为:-Dfile.encoding=UTF-8您提到的Icarin:

此变量为:JAVA_OPTS,值为:-Dfile.encoding=UTF-8

这是在/conf/server.xml中吗

大家好

你提到的伊卡林:

此变量为:JAVA_OPTS,值为:-Dfile.encoding=UTF-8

这是在/conf/server.xml中吗


大家好

我做了您建议的修改,程序在Eclipse上运行良好,但是直接在windows上没有任何更改。我已经找到了答案(更多细节请参见这个问题的答案)。我做了您建议的修改,程序在Eclipse上运行良好,但是直接在windows上没有任何更改。我已经找到了答案(有关更多详细信息,请参阅此问题的答案)。这是Sun/Oracle JVM特定的设置,用于将编码设置为读取文件(因此在其他JVM中不起作用)。换句话说,
数据控制器.translateFile(documentBuffer)
可能没有使用UTF-8来处理文本文件。这是Sun/Oracle JVM特定的设置,用于将编码设置为读取文件(因此在其他JVM中不起作用)。换句话说,
数据控制器.translateFile(documentBuffer)
可能没有使用UTF-8来处理文本文件。