【Servlet】Response的OutputStream与Writer输出数据乱码的问题
OutputStream输出中文数据乱码问题解决方式:
package cn.lsh.servlet; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //Response的OutputStream输出中文数据问题 public class Response extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //指定以UTF-8编码,那么就要指定浏览器以UTF-8解码,总之,编码与解码要相同,否者就会出现乱码。 //注意:第二个参数中间不要写成“,”否则当访问Servlet时会出现提示下载的情况。 response.setHeader("content-type", "text/html;charset=utf-8"); String str = "中国,我爱你"; OutputStream out = response.getOutputStream(); out.write(str.getBytes("utf-8"));//指定以UTF-8编码。 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Writer输出数据乱码的问题解决方法:
以下是没有指定浏览以UTF-8码表打开所以导致浏览器输出乱码:
package cn.lsh.servlet; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //Response的OutputStream输出中文数据问题 public class Response extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //指定以UTF-8编码,那么就要指定浏览器以UTF-8解码,总之,编码与解码要相同,否者就会出现乱码。 //注意:第二个参数中间不要写成“,”否则当访问Servlet时会出现提示下载的情况。 //设置response使用utf-8码表,以控制response以什么码表向浏览器写出数据 response.setCharacterEncoding("UTF-8"); /* //指定浏览器以什么码表打开服务器发送的数据 response.setContentType("text/html;charset=utf-8"); */ String str = "中国,我爱你"; PrintWriter pw = response.getWriter(); pw.write(str); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
浏览器输出结果为:

以下指定浏览以UTF-8码表打开所以浏览器输出中文数据正常:
package cn.lsh.servlet; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //Response的OutputStream输出中文数据问题 public class Response extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //指定以UTF-8编码,那么就要指定浏览器以UTF-8解码,总之,编码与解码要相同,否者就会出现乱码。 //注意:第二个参数中间不要写成“,”否则当访问Servlet时会出现提示下载的情况。 //设置response使用utf-8码表,以控制response以什么码表向浏览器写出数据 response.setCharacterEncoding("UTF-8"); //指定浏览器以什么码表打开服务器发送的数据 response.setContentType("text/html;charset=utf-8"); String str = "中国,我爱你"; PrintWriter pw = response.getWriter(); pw.write(str); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/

浙公网安备 33010602011771号