欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

request.getInputStream()乱码问题解决方案(未解决)

程序员文章站 2024-02-03 11:17:16
...

request.getInputStream()乱码问题解决方案

学习request和response时候遇到乱码问题,设置request.setCharacterEncoding(“UTF-8”)也不管用。

源代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		ServletInputStream sis = request.getInputStream();
		int len = 0;
		byte[] b = new byte[1024];
		while((len = sis.read(b))!=-1) {
			System.out.println(new String(b,0,len));
		}
		sis.close();
	}

结果依然乱码:

username=tom&pwd=222&sex=%E7%94%B7&hobby=%E7%AF%AE%E7%90%83&city=bj

根据网上资料,
改成如下代码:

request.setCharacterEncoding("UTF-8");
StringBuilder buffer = new StringBuilder();
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader
		(request.getInputStream(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
	buffer.append(line);
}
reader.close();
System.out.println(buffer);

结果依然不行。。。

先做记录,以后再来探究,也请大牛赐教。(抱拳了