Servlet的response乱码问题

一.response有两种输出流(编码:UTF-8):

1.字节流:response.getOutputStream().write(date.getBytes("UTF-8"));

//让浏览器用utf8来解析返回的数据
response.setHeader("Content-type", "text/html;charset=UTF-8");
String data = "测试乱码";
response.getOutputStream().write(data.getBytes("utf-8"));

 

2.字符流:response.getWrite().write();需要设置response.setCharacterEncoding("UTF-8");

//1.告诉servlet服务器端用UTF-8转码,而不是用默认的ISO8859-1
response.setCharacterEncoding("UTF-8");
//2.让浏览器用utf8来解析返回的数据
response.setHeader("Content-type", "text/html;charset=UTF-8");
String data = "测试乱码";
response.getWrite().write(data);

PS:

1. 如果中文返回出现????字符,这表明没有加response.setCharacterEncoding("UTF-8");这句话。

2. 如果返回的中文是“烇湫”这种乱码,说明浏览器的解析问题,应该检查下是否忘加response.setHeader("Content-type", "text/html;charset=UTF-8");这句话。

 

引用:https://blog.csdn.net/TLMS_/article/details/78749980

posted @ 2019-10-20 01:03  AyeeX  阅读(309)  评论(0编辑  收藏  举报