Java 如何通过 ServletOutputStream 以 UTF-8 编码发送字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1992400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send through ServletOutputStream characters in UTF-8 encoding
提问by GyRo
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 输出流的字符集???
采纳答案by Brian Agnew
I think you want to use getWriter()instead. That will accept a string and encode it, whereas the output stream is for handling binarydata.
我认为您想改用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.
Either this method or getOutputStream() may be called to write the body, not both.
返回一个可以向客户端发送字符文本的 PrintWriter 对象。使用的字符编码是 setContentType(java.lang.String) 方法的 charset= 属性中指定的字符编码,必须在调用该方法之前调用该字符集才能生效。
可以调用此方法或 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...);
回答by Chang
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();
回答by quagmired
This also works:
这也有效:
ServletOutputStream out = response.getOutputStream();
out.write("MY-UTF-8 CODE".getBytes("UTF-8"));
回答by Allen
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();
}
回答by ???
// HTML Output code list
StringBuffer select_code = new StringBuffer();
List<con_element> ccc = codeService.code_select(code);
for(int i=0;i<ccc.size();i++){
select_code.append("<option value='" + ccc.get(i).getCce_num() + "'>" + ccc.get(i).getCce_hname() + "</option>" );
}
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getWriter().print( select_code );
回答by QA Specialist
In the case where you have to Request Dispatcher from a doFilter to @WebServlet("/anywebpage") which should only contain the PrintWriter out = response.getWriter();
如果您必须从 doFilter 向 @WebServlet("/anywebpage") 请求 Dispatcher,它应该只包含 PrintWriter out = response.getWriter();
String erpg = "anywebpage";
response.setContentType("text/html; charset=UTF-8");
RequestDispatcher rd = request.getRequestDispatcher(erpg);
rd.include(request, response);
You should NOT require to declare the following line: response.setCharacterEncoding("UTF-8");
您不应该要求声明以下行: response.setCharacterEncoding("UTF-8");
Instead, make sure to include the following before the PrintWriter out = response.getWriter(); instruction:
相反,请确保在 PrintWriter out = response.getWriter(); 之前包含以下内容。操作说明:
response.setContentType("text/html; charset=UTF-8");