问题描述
最近写了一个简单的注册页面,页面的验证码是通过后台Java图形化工具画出来的四字成语,本地测试效果如下:
当时当挂到云服务器上面运行过后却出现了验证码乱码问题,中文全部显示为框框:
原因分析
可能是因为云服务器上面没有安装中文支持,或者是没有相应的字体支持。
解决方案
1、locale命令:查看当前系统语言环境
root@VM-148-108-ubuntu:/home/ubuntu# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
显示local缺失文件,那么就看看系统安装了那些local。
2、lacale -a命令:查看系统内安装的locale
root@VM-148-108-ubuntu:/home/ubuntu# locale -a
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_COLLATE to default locale: No such file or directory
C
C.UTF-8
POSIX
en_US.utf8
发现没有zh_CN.UTF-8的中文locale。
3、sudo locale-gen zh_CN.UTF-8命令:安装zh_CN.UTF-8
root@VM-148-108-ubuntu:/home/ubuntu# sudo locale-gen zh_CN.UTF-8
Generating locales...
zh_CN.UTF-8... done
Generation complete.
安装完成。
4、locale -a命令:再次查看,系统语言安装成功
root@VM-148-108-ubuntu:/home/ubuntu# locale -a
C
C.UTF-8
en_US.utf8
POSIX
zh_CN.utf8
5、locale命令:查看语言环境
root@VM-148-108-ubuntu:/home/ubuntu# locale
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
正常。
5、sudo apt-get install language-pack-zh-hans命令:安装中文语言包。
root@VM-148-108-ubuntu:/home/ubuntu# sudo apt-get install language-pack-zh-hans
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
将会安装下列额外的软件包:
language-pack-zh-hans-base
下列【新】软件包将被安装:
language-pack-zh-hans language-pack-zh-hans-base
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 231 个软件包未被升级。
需要下载 1,950 kB 的软件包。
解压缩后会消耗掉 7,762 kB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://mirrors.tencentyun.com/ubuntu/ trusty-updates/main language-pack-zh-hans-base all 1:14.04+20160720 [1,948 kB]
获取:2 http://mirrors.tencentyun.com/ubuntu/ trusty-updates/main language-pack-zh-hans all 1:14.04+20160720 [1,866 B]
下载 1,950 kB,耗时 0秒 (7,404 kB/s)
Selecting previously unselected package language-pack-zh-hans-base.
(正在读取数据库 ... 系统当前共安装有 85947 个文件和目录。)
Preparing to unpack .../language-pack-zh-hans-base_1%3a14.04+20160720_all.deb ...
Unpacking language-pack-zh-hans-base (1:14.04+20160720) ...
Selecting previously unselected package language-pack-zh-hans.
Preparing to unpack .../language-pack-zh-hans_1%3a14.04+20160720_all.deb ...
Unpacking language-pack-zh-hans (1:14.04+20160720) ...
正在设置 language-pack-zh-hans (1:14.04+20160720) ...
正在设置 language-pack-zh-hans-base (1:14.04+20160720) ...
Generating locales...
zh_CN.UTF-8... up-to-date
zh_SG.UTF-8... done
Generation complete.
中文出现。
6、安装字体:宋体
将c:\windows\Fonts下的字体文件复制到云服务器上:
cp *.ttf *.TTF /home/username/win/
安装字体:
mv /home/username/win/ /usr/share/font/
刷新:
sudo fc-cache -fv
从起服务器后生效。
完成效果