Linux环境下中文字体乱码处理办法

时间:2023-03-09 02:30:02
Linux环境下中文字体乱码处理办法

项目中包含了一项生成JPG图片的功能,该功能需要使用JAVA的Graphics2D动态生成图片中的文字,原来在Windows平台中没有发现问题,但是迁移至Linux平台后发现生成的中文变成了乱码。

百度发现是因为Linux下没有相应的字体引起的,解决办法如下:

1、找到Windows平台下的字体文件;

常规路径为:C:\Windows\Fonts

宋体文件为:simsun.ttc

楷体文件为:simkai.ttf

2、找到Linux平台下JDK的字体目录;

本系统路径为:/opt/IBM/WebSphere/AppServer/java_1.7_64/jre/lib/fonts

该路径下存储了默认的字体。

[root@appsrv01 fonts]# pwd
/opt/IBM/WebSphere/AppServer/java_1.7_64/jre/lib/fonts
[root@appsrv01 fonts]#
[root@appsrv01 fonts]# ll
total 31460
-rwxr-xr-x 1 root root 4041 Mar 24 2014 fonts.dir
-rwxr-xr-x 1 root root 75144 Mar 24 2014 LucidaBrightDemiBold.ttf
-rwxr-xr-x 1 root root 75124 Mar 24 2014 LucidaBrightDemiItalic.ttf
-rwxr-xr-x 1 root root 80856 Mar 24 2014 LucidaBrightItalic.ttf
-rwxr-xr-x 1 root root 344908 Mar 24 2014 LucidaBrightRegular.ttf
-rwxr-xr-x 1 root root 317896 Mar 24 2014 LucidaSansDemiBold.ttf
-rwxr-xr-x 1 root root 698236 Mar 24 2014 LucidaSansRegular.ttf
-rwxr-xr-x 1 root root 234068 Mar 24 2014 LucidaTypewriterBold.ttf
-rwxr-xr-x 1 root root 242700 Mar 24 2014 LucidaTypewriterRegular.ttf

3、将需要的字体拷贝至此目录即可。

[root@appsrv01 fonts]# ll
total 31460
-rwxr-xr-x 1 root root 4041 Mar 24 2014 fonts.dir
-rwxr-xr-x 1 root root 75144 Mar 24 2014 LucidaBrightDemiBold.ttf
-rwxr-xr-x 1 root root 75124 Mar 24 2014 LucidaBrightDemiItalic.ttf
-rwxr-xr-x 1 root root 80856 Mar 24 2014 LucidaBrightItalic.ttf
-rwxr-xr-x 1 root root 344908 Mar 24 2014 LucidaBrightRegular.ttf
-rwxr-xr-x 1 root root 317896 Mar 24 2014 LucidaSansDemiBold.ttf
-rwxr-xr-x 1 root root 698236 Mar 24 2014 LucidaSansRegular.ttf
-rwxr-xr-x 1 root root 234068 Mar 24 2014 LucidaTypewriterBold.ttf
-rwxr-xr-x 1 root root 242700 Mar 24 2014 LucidaTypewriterRegular.ttf
-rwxrwxr-x 1 root root 11785184 Nov 29 03:12 simkai.ttf
-rw-r--r-- 1 root root 18259888 Nov 29 03:15 simsun.ttc

4、重启应用程序服务器后验证功能,发现已恢复正常。