Tomcat的JspWriter编码不正确
我有 Tomcat 7 的默认设置,并且所有与 java 相关的内容都配置为使用 utf-8。
这不起作用(utf-8 字符被破坏):
<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%
URL target = new URL("http://en.wikipedia.org/wiki/Main_Page");
Reader input = new BufferedReader(new InputStreamReader(target.openStream()));
StringWriter buffer = new StringWriter();
char[] chrs = new char[1024 * 4];
int n = 0;
while (-1 != (n = input.read(chrs)))
{
buffer.write(chrs, 0, n);
}
StringReader reader = new StringReader(buffer.toString());
n = 0;
while (-1 != (n = reader.read(chrs)))
{
out.write(chrs, 0, n);
}
%>
这可以,但会记录 IllegalStateExceptions:
<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%
URL target = new URL("http://en.wikipedia.org/wiki/Main_Page");
Reader input = new BufferedReader(new InputStreamReader(target.openStream()));
StringWriter buffer = new StringWriter();
char[] chrs = new char[1024 * 4];
int n = 0;
while (-1 != (n = input.read(chrs)))
{
buffer.write(chrs, 0, n);
}
StringReader reader = new StringReader(buffer.toString());
OutputStreamWriter output = new OutputStreamWriter(response.getOutputStream());
n = 0;
while (-1 != (n = reader.read(chrs)))
{
output.write(chrs, 0, n);
}
%>
我一直在搜索但没有找到答案。这是 Tomcat 中的错误,还是我遗漏了什么?
I have a default setup of Tomcat 7 and everything java-related configured to use utf-8.
This does not work (utf-8 characters are mangled):
<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%
URL target = new URL("http://en.wikipedia.org/wiki/Main_Page");
Reader input = new BufferedReader(new InputStreamReader(target.openStream()));
StringWriter buffer = new StringWriter();
char[] chrs = new char[1024 * 4];
int n = 0;
while (-1 != (n = input.read(chrs)))
{
buffer.write(chrs, 0, n);
}
StringReader reader = new StringReader(buffer.toString());
n = 0;
while (-1 != (n = reader.read(chrs)))
{
out.write(chrs, 0, n);
}
%>
This does, but logs IllegalStateExceptions:
<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%
URL target = new URL("http://en.wikipedia.org/wiki/Main_Page");
Reader input = new BufferedReader(new InputStreamReader(target.openStream()));
StringWriter buffer = new StringWriter();
char[] chrs = new char[1024 * 4];
int n = 0;
while (-1 != (n = input.read(chrs)))
{
buffer.write(chrs, 0, n);
}
StringReader reader = new StringReader(buffer.toString());
OutputStreamWriter output = new OutputStreamWriter(response.getOutputStream());
n = 0;
while (-1 != (n = reader.read(chrs)))
{
output.write(chrs, 0, n);
}
%>
I've been searching but found no answers. Is this a bug in Tomcat, or is there something I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您构造
InputStreamReader
< /a> 不指定字符集作为第二个参数,则将使用平台默认编码,通常是 ISO-8859-1。您需要指定与目标 URL 的响应标头中指定的字符集相同的字符集,即 UTF-8。导致
IllegalStateException
的原因是您在 JSP 而不是 Servlet 中执行此操作。 JSP 内部使用response .getWriter()
,但您正在调用 JSP scriptlet 中的response.getOutputStream()
。正如 javadoc 中所解释的,这不能同时完成。When you construct
InputStreamReader
without specifying a charset as 2nd argument, then the platform default encoding will be used, which is often ISO-8859-1. You need to specify the same charset as specified in the response header of the target URL, which is UTF-8.The
IllegalStateException
is caused because you're doing this in a JSP instead of a Servlet. The JSP internally usesresponse.getWriter()
, but you're callingresponse.getOutputStream()
in a JSP scriptlet. This cannot be done simultaneously as explained in their javadocs.