解决HttpServletResponse输出的中文乱码问题
2018-02-24 11:21
337 查看
首先,response返回有两种,一种是字节流outputstream,一种是字符流printwrite。申明:这里为了方便起见,所有输出都统一用UTF-8编码。
先说字节流,要输出“中国",给输出流的必须是转换为utf-8的“中国”,还要告诉浏览器,用utf8来解析数据
[html] view plain copy //这句话的意思,是让浏览器用utf8来解析返回的数据
response.setHeader("Content-type", "text/html;charset=UTF-8");
String data = "中国";
OutputStream ps = response.getOutputStream();
//这句话的意思,使得放入流的数据是utf8格式
ps.write(data.getBytes("UTF-8"));
再说字符流,要输出中国,需要设置response.setCharacterEncoding("UTF-8");[html] view plain copy //这句话的意思,是让浏览器用utf8来解析返回的数据
response.setHeader("Content-type", "text/html;charset=UTF-8");
//这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859
response.setCharacterEncoding("UTF-8");
String data = "中国";
PrintWriter pw = response.getWriter();
pw.write(data);
经验:1,如果中文返回出现??字符,这表明没有加response.setCharacterEncoding("UTF-8");这句话。 2,如果返回的中文是“烇湫”这种乱码,说明浏览器的解析问题,应该检查下是否忘加response.setHeader("Content-type", "text/html;charset=UTF-8");这句话。
如果上面都解决不了,请看更详细的说明 http://blog.csdn.net/kontrol/article/details/7767983
先说字节流,要输出“中国",给输出流的必须是转换为utf-8的“中国”,还要告诉浏览器,用utf8来解析数据
[html] view plain copy //这句话的意思,是让浏览器用utf8来解析返回的数据
response.setHeader("Content-type", "text/html;charset=UTF-8");
String data = "中国";
OutputStream ps = response.getOutputStream();
//这句话的意思,使得放入流的数据是utf8格式
ps.write(data.getBytes("UTF-8"));
再说字符流,要输出中国,需要设置response.setCharacterEncoding("UTF-8");[html] view plain copy //这句话的意思,是让浏览器用utf8来解析返回的数据
response.setHeader("Content-type", "text/html;charset=UTF-8");
//这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859
response.setCharacterEncoding("UTF-8");
String data = "中国";
PrintWriter pw = response.getWriter();
pw.write(data);
经验:1,如果中文返回出现??字符,这表明没有加response.setCharacterEncoding("UTF-8");这句话。 2,如果返回的中文是“烇湫”这种乱码,说明浏览器的解析问题,应该检查下是否忘加response.setHeader("Content-type", "text/html;charset=UTF-8");这句话。
如果上面都解决不了,请看更详细的说明 http://blog.csdn.net/kontrol/article/details/7767983
相关文章推荐
- 解决HttpServletResponse输出中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- [转] 解决HttpServletResponse输出的中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- 【转】解决HttpServletResponse输出的中文乱码问题
- 解决HttpServletResponse输出的中文乱码问题
- response实现案例之向页面输出中文解决乱码问题
- response解决控制台输出没问题,但前台传递乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- IntelliJ IDEA控制台输出中文乱码问题解决方法
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- request、response 中文乱码问题与解决方式
- 文件下载 response.setHeader()下载中文文件名乱码问题 解决办法
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题