1、首先先回忆自身项目的编码格式,即在本地进行编码时使用的编码格式。UTF-8
2、检测tomcat的设置问题,在web.xml和server中的设置:
server.xml中:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"
URIEncoding="UTF-8" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
web.xml中以下内容去掉注释:
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<async-supported>true</async-supported>
</filter>
3、检测ubuntu服务器中的编码格式
//检测本地语言环境:
locale
//进行配置的修改
vi /etc/profile
//添加下列行(LC_ALL表示全部设置,但不影响LANG):
export LANG="en_US.UTF-8"
export LC_ALL="zh_CN.UTF-8"
//设置后使其起效
source /etc/profile
//当出现 cannot 错误时 ,使用以下命令进行安装相应语言包
sudo locale-gen zn_CN.UTF-8
//当起效后,要再次进入配置中修改,将刚添加的两行删除,不然会影响jdk环境
4、mysql的配置
1.修改mysql默认配置:
默认的mysql配置:server、client都是latin1,对中文是不支持的,这里改为utf8
方法:
修改配置文件:
vim /etc/mysql/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
重启mysql
/etc/init.d/mysql restart
检查编码方式:
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
2.修改项目使用的数据库的配置
修改此数据库的编码方式为utf8(默认是latin1)
mysql> alter database 数据库名 character set utf8;
查看此数据库的编码:
mysql> status
Connection id: 41
Current database: tslcrms
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 53 min 14 sec
mysql> show create database tslcrms;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| tslcrms | CREATE DATABASE 数据库名
/*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------------+
3.导入数据库文件中的表、字段的编码方式都修改成utf8
可以直接改.sql文件,改完导入。
也可以先导入,再用命令查看和更改。
附:在mysql中使用命令修改字符集:
mysql> SET character_set_client = utf8 ;
mysql> SET character_set_connection = utf8 ;
mysql> SET character_set_database = utf8 ;
mysql> SET character_set_results = utf8 ;
mysql> SET character_set_server = utf8 ;
mysql> SET collation_connection = utf8 ;
mysql> SET collation_database = utf8 ;
mysql> SET collation_server = utf8 ;
SET NAMES 'utf8';
相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
5、另外
mysql默认的字符集为latin1的,所以要改为utf8的。很多网上的文章执行“sudo vi /etc/mysql/my.cnf”,可是打开一看,里面就两行话:
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
想来mysql的配置文件应该是在那两个文件夹下面,于是尝试之下打开了/etc/mysql/mysql.conf.d/ 下的mysqld.cnf文件,执行下面的命令:
~$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
[client]
default-character-set=utf8
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
后重启Mysql服务器
~$ sudo /etc/init.d/mysql restart
~$ mysql -u root -p
mysql> show variables like "%char%"
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
欢迎加入学习交流群569772982,大家一起学习交流。