您的位置 首页 java

Java常见的乱码问题及解决方案

本文将介绍一种 java Web/Api 开发常见的乱码问题。

一、 JSP 页面乱码

因为网页默认的 编码 格式为 ISO-8859-1 ,该编码格式不支持汉字,所以在网页上显示为乱码???.

解决方案

在文件顶部添加 header 上添加

<pre>

<%@ page contentType=”text/html;charset= utf-8 ” language=”java” %>

</pre>

二、 ajax 请求时 request .getParams乱码

在你的tomcat server.xml中配置

 <Connector port="8080"  protocol ="HTTP/1.1" 

</pre>

即添加

如果你想学习java可以来这个群,五三二,二五九,九五二,里面有大量的学习资料可以下载。 URIEconding="UTF-8" useBody encoding ForURI="true"

三、ajax请求时 response出去为乱码

在使用Spring mvc 时 须在web.xml 配置

<filter> 

mvc注释 RequestMapping 声明 增加utf-8声明如

@RequestMapping(method = RequestMethod.POST, produces = "application/ json ; charset=utf-8")

如果是普通的 servlet 时,则须代码编码之后输出

response.setContentType("text/html;charset=UTF-8"); 

三、文件乱码

经常在拿到别人的工程文件时,由于他不同的编码例如 GBK 之类的编码,需要在IDE中将文件编码统一成UTF-8 即可.

window平台下,可以实用 notepad++ 进行编转化替换即可。

四、终端乱码

如果是 System.out.println 打印显示乱码 则 VM options 里 添加 -Dfile.encoding=UTF-8 参数

五、数据库乱码

  • jdbc 链接utf-8声明

jdbc: mysql ://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8

  • 新建数据库时,选择utf-8/utf-8mb4编码

六、 emoji 乱码支持

  • 新建数据库选择utf-8mb4

  • 数据库的字符集配置 参考mysql/Java服务端对emoji的支持

  • mysql-connector-java 版本升级至 5.1.22,此版本笔者亲测 Ok!

如果你想学习java可以来这个群 532259952

文章来源:智云一二三科技

文章标题:Java常见的乱码问题及解决方案

文章地址:https://www.zhihuclub.com/181632.shtml

关于作者: 智云科技

热门文章

评论已关闭

6条评论

  1. Thrombotic microangiopathy might further contribute to both acute and chronic nephrotoxicity

  2. Surgery went so well that I wanted to walk through the park home after and my family had to fight me to get in a cab

  3. vigora fluticasone salmeterol brand name philippines The bone chilling allegations in today s indictment read like they were ripped from the pages of a Tom Clancy novel, said US Attorney Preet Bharara

网站地图