我有一个简单的 Java 应用程序,它将一些 HTML
代码写入 OutputStream
。
运行此命令并打开与 Chromium 或 Opera 的连接效果非常好,而对于 Firefox,HTML 代码不会按原样解释和显示。
包含代码的字符串如下所示:
HTMLCode = "<!DOCTYPE html>\n<html>\n<body>\n<h1>\n"
+ "The sum of " + operand1 + " and " + operand2 + " is " + result
+ "\n</h1>\n</body>\n</html>";
其中 operand1
、operand2
和 result
是 String
。
我通过以下代码写入OutputStream
:
new PrintStream(out).println(HTMLCode);
Chromium 和 Opera 正确显示,例如:
10 和 20 之和是 30
Firefox 显示
<!DOCTYPE html>
<html>
<body>
<h1>
The sum of 2 and 34 is 36
</h1>
</body>
</html>
最佳答案
在实际内容之前发送正确的 HTML header 。
PrintStream ps = new PrintStream(out);
DateFormat df = new SimpleDateFormat("EEE, MMM d, yyyy HH:mm:ss z");
ps.println("HTTP/1.1 200 OK");
ps.println("Content-Type: text/html; charset=UTF-8");
ps.println("Date: " + df.format(new Date()));
ps.println("Connection: close");
ps.println();
ps.println(HTMLCode);
关于java - 通过 Java 中的 OutputStream 发送到 Firefox 的 HTML 代码无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559915/