Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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 使用servlet的UTF-8响应_Java_Servlets_Utf 8 - Fatal编程技术网

Java 使用servlet的UTF-8响应

Java 使用servlet的UTF-8响应,java,servlets,utf-8,Java,Servlets,Utf 8,我从Servlet中的Perl页面读取HTTP响应,如下所示: public String getHTML(String urlToRead) { URL url; HttpURLConnection conn; BufferedReader rd; String line; String result = ""; try { url = new URL(urlToRead);

我从Servlet中的Perl页面读取HTTP响应,如下所示:

public String getHTML(String urlToRead) {
        URL url;
        HttpURLConnection conn;
        BufferedReader rd;
        String line;
        String result = "";
        try {
           url = new URL(urlToRead);
           conn = (HttpURLConnection) url.openConnection();
           conn.setRequestMethod("GET");
           conn.setRequestProperty("Accept-Charset", "UTF-8");
           conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");

           rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
           while ((line = rd.readLine()) != null) {
              byte [] b = line.getBytes();
              result += new String(b, "UTF-8");
           }
           rd.close();
        } catch (Exception e) {
           e.printStackTrace();
        }
        return result;
   }
我使用以下代码显示此结果:

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

        PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"), true);


        try {

            String query = request.getParameter("query");
            String type = request.getParameter("type");

            String res = getHTML(url);
            out.write(res);

        } finally {            
            out.close();
        }
但是响应仍然没有编码为UTF-8。我做错了什么


提前感谢。

调用
line.getBytes()
看起来可疑。如果您确定返回的内容是UTF-8编码的,那么可能应该将其设置为line.getBytes(“UTF-8”)。此外,我甚至不知道为什么它是必要的。从
BufferedReader
获取数据的典型方法是使用
StringBuilder
继续将从
readLine
检索到的每个
字符串添加到结果中。不需要在
字符串
字节[]
之间来回转换

result
更改为
StringBuilder
,然后执行以下操作:

while ((line = rd.readLine()) != null) {
    result.append(line);
}

这里是打破字符编码转换链的地方:

       while ((line = rd.readLine()) != null) {
          byte [] b = line.getBytes();  // NOT UTF-8
          result += new String(b, "UTF-8");
       }
从字符串#getBytes()javadoc:

使用平台的 默认字符集,将结果存储到新的字节数组中

而且,defaullt字符集可能不是UTF-8


但是,为什么所有的转换都放在首位呢?只需从源读取原始字节,然后将原始字节写入使用者。应该一直都是UTF-8

我在另一个场景中也遇到了同样的问题,但只要这样做,我相信它会起作用:

byte[] b = line.getBytes(UTF8_CHARSET);
在while循环中:

while ((line = rd.readLine()) != null) {
          byte [] b = line.getBytes();  // NOT UTF-8
          result += new String(b, "UTF-8");
       }

在我的例子中,我确实添加了另一个配置

以前,我是这样写的:

try (PrintStream printStream = new PrintStream(response.getOutputStream()) {
        printStream.print(pageInjecting);
}
我改为:

try (PrintStream printStream = new PrintStream(response.getOutputStream(), false, "UTF-8")) {
        printStream.print(pageInjecting);
}

在您的PrintWriter中,是否可能“UTF8”不存在,但“UTF-8”存在?我试图更改它,但没有结果(已编辑)。您如何判断它不是UTF-8?您有什么证据证明
urlToRead
正在以UTF-8返回其内容?因为这就是你所假设的。当您已经有一个
字符串时,为什么要将
转换为
字节
?+1用于
StringBuilder
。您目前的速度非常慢:我最初使用的是StringBuilder。但是因为它不起作用,我试着根据论坛上的帖子建议修改代码。好的。我把它改回了StringBuilder,现在可以使用了。我不知道为什么它以前不起作用。非常感谢你的回答+1.