首页天道酬勤java数据分析框架(夸一个人分析的透彻)

java数据分析框架(夸一个人分析的透彻)

admin 12-21 04:49 249次浏览

前言

乱码是我们在程序开发中经常遇到的事,让人头疼。 特别是做javaweb开发。 如果乱码的发生原理不明确,那么遇到乱码的问题就会摸不着头脑,无法处理。

乱码主要出现在以下两个部分。

第一,浏览器通过表单提交到后台。 如果表格内容有中文的话,在后台接收的数据可能会乱码。

第二,后端服务器需要返回到浏览器数据。 如果数据包含中文,浏览器可能会显示乱码。

接下来,让我们逐一分析一下乱码的产生原因和解决乱码问题的方法。

一、后端收到浏览器提交的中文乱码

在此分为get请求和post请求。

获取请求

get请求的请求参数中包含中文,后台接收时会发生乱码。 由于tomcat的默认编码为“ISO-8859-1”,因此tomcat使用“ISO-8859-1”对中文进行编码。 因为这个编码不支持中文,所以在后台接收时会乱码。 有两种解决方法。

param=新字符串(param.getbytes (iso-8859-1 ),) utf-8 ) ); 将tomcat代码更改为“utf-8”。 不推荐使用这种方法。 开机自检请求

post请求、乱码的原因与get请求相同,解决方法比较简单,如下所示。

request.setcharacterencoding (utf-8 ); 如果将请求参数的编码格式设定为“utf-8”,就没有问题。

二、后端返回中文给浏览器发生乱码

后端向浏览器返回数据。 一般有两种格式: response.getOutputStream (和response.getWriter )。

两者的区别及使用规则

获取输出流) )是指获取了用于向客户端(浏览器)输出数据的输出流。 如果输出了字符,则转换为二进制输出,如果字符出现了中文,则为“Java.io.charconversionexception 336666”,因此调用requonse.getWriter ()方法时会生成文本字符串因此,如果要输出二进制数据(如图像),则必须使用response.getOutputStream。

getOutputStream () )和getWriter ) )请不要同时使用。 如果不使用,则抛出“获取写入器”Hasalreadybeencalledforthisresponse )异常。

讲完了,下面主要就通过实践分析产生乱码的原理进行说明。

response.get输出流() .打印) )。

别说回英语数据,没什么问题,回中文是什么效果?

@ request映射(/hello world.do ) )。

公共语音助手世界(http服务器请求,http服务器响应)。

String str='中国加油,武汉加油';

response.getOutputStream () .打印) str;

}结果如下。

分析:

由于OutPutStream输出二进制数据,因此需要将字符串更改为二进制输出。 Tomcat使用“ISO8859-1”编码转换它,但是中文不支持“ISO859-1”,所以我们抛出例外。

response.get输出流. write (

同样,让我们看看输出中文会怎么样。

@ request映射(/hello world.do ) )。

公共语音助手世界(http服务器请求,http服务器响应)。

String str='中国加油,武汉加油';

response.getOutputStream ().write(str.getbytes ) );

}页面的输出结果如下。

涓锷犳补锞屌姹娌分析:

在java中,String的getBytes ) )方法是获得操作系统的缺省编码格式的字节数组。 我的电脑系统是macos,默认编码格式是utf-8,返回给浏览器的是utf-8编码格式的字节数组,但浏览器的默认是' gbk '编码

解析,所以就乱码了。

既然这样,那我们换成“gb2312”编码(gb2312编码是gbk编码的一种)试试呢?

@RequestMapping("/helloworld.do") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中国加油,武汉加油"; response.getOutputStream().write(str.getBytes()); }

页面输出:

中国加油,武汉加油

原理我们弄清楚了,但是在项目开发中,我们需要编码统一,最常用的就是中文字符编码"UTF-8",可是按照我们的理解,如果我们直接response.getOutputStream().write(str.getBytes("utf-8"));肯定会乱码,我们需要用某种方式,告诉浏览器,你要用我指定的“utf-8”编码接受我返回的中文。response.setContentType("text/html;charset=UTF-8")这样就完事了,看看效果吧。

@RequestMapping("/helloworld.do") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中国加油,武汉加油"; response.setContentType("text/html;charset=utf-8"); response.getOutputStream().write(str.getBytes("utf-8")); }

页面输出:

中国加油,武汉加油

response.getWriter()

前面已经总结过了,response.getWriter()跟response.getOutputStream()不一样,outputStream是输出二进制的,writer是输出字符串的。response.getWriter()输出也有两种方法,一种是print(),一种是write(),其实两者在处理乱码这一块没有什么区别,就不分开讲述了。

示例:

@RequestMapping("/helloworld.do") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中国加油,武汉加油"; response.getWriter().print(str); }

页面输出:

?????????

分析:

同样的,Tomcat默认的编码是ISO 8859-1,当我们输出中文数据的时候,Tomcat会依据ISO 8859-1码表给我们的数据编码,中文不支持这个码表呀,所以出现了乱码。

这个时候response.setContentType("text/html;charset=UTF-8")又派上用场了。

@RequestMapping("/helloworld.do") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中国加油,武汉加油"; response.setContentType("text/html;charset=utf-8"); response.getWriter().print(str); }

页面输出:

中国加油,武汉加油

在这里,response.setContentType("text/html;charset=UTF-8")做了两件事,response.setCharacterEncoding("UTF-8");和response.setHeader("Content-Type", "text/html;charset=UTF-8");具体就是,第一,输出中文”中国加油,武汉加油“的时候,对中文进行”utf-8“编码;第二,告诉浏览器,你也要用"utf-8"来显示我返回的中文。

最后

对于springMVC项目,如何解决乱码问题呢?项目中一般会在web.xml中配置编码过滤器。配置如下:

<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.sdcjl</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

这样能保证请求的参数按照指定的编码格式进行编码,简单翻看下过滤器源码如下:

@Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); }

代码中有两处重要的地方值得注意,分别是request.setCharacterEncoding(this.encoding);和response.setCharacterEncoding(this.encoding);前者表示我们对请求过来的参数使用指定的"utf-8"进行编码,后者便是,返回给浏览器时,后端返回字符的编码是“utf-8”。

好了,经过以上分析是不是乱码也没有那么可怕了。只要明白其中的缘由,解决起来就是一行代码或者几行配置的事儿了,如果大家觉得有帮助,不妨点赞支持一下?

云手机服务器 云手机服务器 UPhoneServer怎么用vue实现动态路由
java编写dll(java显示乱码) 钉钉密聊有什么特点(提高对话能力)
相关内容