My servlet code looks like that:
我的servlet代码看起来像这样:
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
ServletOutputStream out = response.getOutputStream();
out.println(...MY-UTF-8 CODE...);
...
...
then I get the error:
然后我得到错误:
java.io.CharConversionException: Not an ISO 8859-1 character: ש
javax.servlet.ServletOutputStream.print(ServletOutputStream.java:89)
javax.servlet.ServletOutputStream.println(ServletOutputStream.java:242)
rtm.servlets.CampaignLogicServlet.doPost(CampaignLogicServlet.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
How can I switch the charset of Servlet's outputstream ???
如何切换Servlet的输出流的字符集???
4 个解决方案
#1
81
I think you want to use getWriter() instead. That will accept a string and encode it, whereas the output stream is for handling binary data.
我想你想要使用getWriter()。这将接受一个字符串并对其进行编码,而输出流则用于处理二进制数据。
From the doc:
从文档:
Returns a PrintWriter object that can send character text to the client. The character encoding used is the one specified in the charset= property of the setContentType(java.lang.String) method, which must be called before calling this method for the charset to take effect.
返回可以将字符文本发送到客户端的PrintWriter对象。使用的字符编码是setContentType(java.lang.String)方法的charset =属性中指定的字符编码,必须在调用此方法之前调用该方法才能使字符集生效。
Either this method or getOutputStream() may be called to write the body, not both.
可以调用此方法或getOutputStream()来编写正文,而不是两者。
Here's the change of the code:
这是代码的变化:
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(...MY-UTF-8 CODE...);
#2
8
This also works:
这也有效:
ServletOutputStream out = response.getOutputStream();
out.write("MY-UTF-8 CODE".getBytes("UTF-8"));
#3
3
The same case happen to me before and i tried to add-on one line on top of the PrintWriter and it is work.
同样的情况发生在我之前,我试图在PrintWriter上添加一行,这是工作。
response.setContentType("text/html; charset=GBK");
PrintWriter out = response.getWriter();
response.setContentType(“text / html; charset = GBK”); PrintWriter out = response.getWriter();
#4
0
public void output(String jsonStr, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=UTF-8;");
response.setCharacterEncoding("UTF-8");
ServletOutputStream out = response.getOutputStream();
out.write(jsonStr.getBytes("UTF-8"));
out.flush();
out.close();
}
#1
81
I think you want to use getWriter() instead. That will accept a string and encode it, whereas the output stream is for handling binary data.
我想你想要使用getWriter()。这将接受一个字符串并对其进行编码,而输出流则用于处理二进制数据。
From the doc:
从文档:
Returns a PrintWriter object that can send character text to the client. The character encoding used is the one specified in the charset= property of the setContentType(java.lang.String) method, which must be called before calling this method for the charset to take effect.
返回可以将字符文本发送到客户端的PrintWriter对象。使用的字符编码是setContentType(java.lang.String)方法的charset =属性中指定的字符编码,必须在调用此方法之前调用该方法才能使字符集生效。
Either this method or getOutputStream() may be called to write the body, not both.
可以调用此方法或getOutputStream()来编写正文,而不是两者。
Here's the change of the code:
这是代码的变化:
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(...MY-UTF-8 CODE...);
#2
8
This also works:
这也有效:
ServletOutputStream out = response.getOutputStream();
out.write("MY-UTF-8 CODE".getBytes("UTF-8"));
#3
3
The same case happen to me before and i tried to add-on one line on top of the PrintWriter and it is work.
同样的情况发生在我之前,我试图在PrintWriter上添加一行,这是工作。
response.setContentType("text/html; charset=GBK");
PrintWriter out = response.getWriter();
response.setContentType(“text / html; charset = GBK”); PrintWriter out = response.getWriter();
#4
0
public void output(String jsonStr, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=UTF-8;");
response.setCharacterEncoding("UTF-8");
ServletOutputStream out = response.getOutputStream();
out.write(jsonStr.getBytes("UTF-8"));
out.flush();
out.close();
}