最近生产环境部分客户反映他们的微博平台发送的微博,图片中有中文乱码现象【这种现象其实不是乱码,而是系统缺少字体库】,如下,部分地区正常。
这套服务在现场环境部署了4台服务器,于是把四台服务上的包拿下来放到我本地测试的时候发现,并没有问题,也就是说,上面的包是正常的,那出问题可能跟环境有关,于是开始定位是哪台服务器环境问题,这个只能用现场环境测试,跟甲方沟通之后,在下班时间,登录系统,将微博配置改成自己的测试账号,逐一排查发现确实是其中一台服务器会有此情况。
找到源码地方,看下使用的字体 微软雅黑:(因此服务在别的服务器正常,所以项目应该是没问题的。代码可以忽略)
public static byte[] getNewImgeUtil(byte[] data,String tit,JSONObject paramJo) {
int imageWidth = 3000; //图片初始的宽度
int imageHeight = 3500; //图片初始的高度
String content = paramJo.getString("content"); //信息内容
String title = paramJo.getString("title"); //信息标题
List<String> contentList = splitString(content, 63);
List<String> titleList = splitString(title, 48);
int H_tip = 300;//图片顶部空白
int btns_2_top = 320;//字符串应呈现位置的y坐标
int btns_left = 200; //内容左边距
int content_line_height = 200; //内容行高
int title_line_height = 220; //标题行高
int imgXY = 1000;
if (data !=null && data.length >0) { //计算图片高度
imageHeight = H_tip + imgXY + ((contentList.size()+2) * content_line_height) + (titleList.size() * title_line_height)+ 100;
}else{
imageHeight = H_tip + ((contentList.size()+2) * content_line_height) + (titleList.size() * title_line_height) ;
}
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
//设置图片的背景色
Graphics2D main = image.createGraphics();
main.setColor(new Color(0, 98, 178));
main.fillRect(0, 0, imageWidth, imageHeight);
//***********************设置下面的提示框
Graphics2D tip = image.createGraphics();
//填充区域并确定区域大小位置
tip.fillRect(0,0, imageWidth, 0);
//***********************设置下面的按钮块
//设置字体颜色,先设置颜色,再填充内容
tip.setColor(Color.white);
tip.drawString("", btns_left, 0);
//设置字体 标题
Font titleFont = new Font("微软雅黑", Font.BOLD, 160);
tip.setFont(titleFont);
//写入标题
for (String str : titleList) {
tip.drawString(str, btns_left, btns_2_top);
btns_2_top = btns_2_top + title_line_height;
}
//设置内容字体
Font tipFont = new Font("微软雅黑", Font.PLAIN, 120);
tip.setFont(tipFont);
tip.drawString("@"+paramJo.getString("orgName"), btns_left, btns_2_top);
btns_2_top = btns_2_top + content_line_height;
tip.drawString(paramJo.getString("happendate"), btns_left, btns_2_top);
btns_2_top = btns_2_top + content_line_height;
//写入内容
for (String str : contentList) {
tip.drawString(str, btns_left, btns_2_top);
btns_2_top = btns_2_top + content_line_height;
}
//插入预警图片
if (data !=null && data.length >0) {
Graphics mainPic = image.getGraphics();
BufferedImage bimg = null;
try {
bimg = ImageIO.read(new ByteArrayInputStream(data));
} catch (Exception e) {}
if(bimg!=null){
mainPic.drawImage(bimg, 1000, btns_2_top +100, imgXY, imgXY, null);
mainPic.dispose();
}
}
return imageToBytes(image,"jpg");
}
具体逻辑没有细看,看到用到的是Graphics2D, 于是查了下资料,想到会不会是服务器字体库少东西,上去看了下果然缺少字体库,于是更新了系统字体库,把别的服务器上字体库文件拷贝过来。
同步字体库:
scp -r /usr/share/fonts root@ip:/usr/share/
在fonts库下同步过来chinese(从别的服务器上scp过来的)
赋予执行权限
[root@n1 chinese]# chmod -R 755 /usr/share/fonts/chinese
[root@n1 chinese]# yum -y install ttmkfdir
已加载插件:fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
mysql | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/3): extras/7/x86_64/primary_db | 236 kB 00:00:00
(2/3): base/7/x86_64/primary_db | 6.1 MB 00:00:00
(3/3): updates/7/x86_64/primary_db | 8.0 MB 00:00:00
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 ttmkfdir.x86_64.0.3.0.9-42.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
=======================================================================================================================================================================================================================================================
Package 架构 版本 源 大小
=======================================================================================================================================================================================================================================================
正在安装:
ttmkfdir x86_64 3.0.9-42.el7 base 48 k
事务概要
=======================================================================================================================================================================================================================================================
安装 1 软件包
总下载量:48 k
安装大小:103 k
Downloading packages:
ttmkfdir-3.0.9-42.el7.x86_64.rpm | 48 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : ttmkfdir-3.0.9-42.el7.x86_64 1/1
验证中 : ttmkfdir-3.0.9-42.el7.x86_64 1/1
已安装:
ttmkfdir.x86_64 0:3.0.9-42.el7
完毕!
[root@n1 chinese]# ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
[root@n1 chinese]# vi /etc/fonts/fonts.conf
[root@n1 chinese]# fc-cache
-bash: fc-cache: 未找到命令
[root@n1 chinese]# yum install fontconfig -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 fontconfig.x86_64.0.2.13.0-4.3.el7 将被 安装
--> 正在处理依赖关系 freetype >= 2.8-7,它被软件包 fontconfig-2.13.0-4.3.el7.x86_64 需要
--> 正在检查事务
---> 软件包 freetype.x86_64.0.2.4.11-11.el7 将被 升级
---> 软件包 freetype.x86_64.0.2.8-14.el7_9.1 将被 更新
--> 解决依赖关系完成
依赖关系解决
=======================================================================================================================================================================================================================================================
Package 架构 版本 源 大小
=======================================================================================================================================================================================================================================================
正在安装:
fontconfig x86_64 2.13.0-4.3.el7 base 254 k
为依赖而更新:
freetype x86_64 2.8-14.el7_9.1 updates 380 k
事务概要
=======================================================================================================================================================================================================================================================
安装 1 软件包
升级 ( 1 依赖软件包)
总下载量:634 k
Downloading packages:
No Presto metadata available for updates
(1/2): fontconfig-2.13.0-4.3.el7.x86_64.rpm | 254 kB 00:00:00
(2/2): freetype-2.8-14.el7_9.1.x86_64.rpm | 380 kB 00:00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计 1.8 MB/s | 634 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在更新 : freetype-2.8-14.el7_9.1.x86_64 1/3
正在安装 : fontconfig-2.13.0-4.3.el7.x86_64 2/3
warning: /etc/fonts/fonts.conf saved as /etc/fonts/fonts.conf.rpmorig
清理 : freetype-2.4.11-11.el7.x86_64 3/3
验证中 : freetype-2.8-14.el7_9.1.x86_64 1/3
验证中 : fontconfig-2.13.0-4.3.el7.x86_64 2/3
验证中 : freetype-2.4.11-11.el7.x86_64 3/3
已安装:
fontconfig.x86_64 0:2.13.0-4.3.el7
作为依赖被升级:
freetype.x86_64 0:2.8-14.el7_9.1
完毕!
然后在etc/share/fonts文件下 font.conf文件里把自己安装的chinese配置进来,如下:<dir>/usr/share/fonts/chinese</dir>
<!-- Font directory list -->
<dir>/usr/share/fonts</dir>
<dir>/usr/share/X11/fonts/Type1</dir> <dir>/usr/share/X11/fonts/TTF</dir>
<dir>/usr/local/share/fonts</dir>
<dir prefix="xdg">fonts</dir>
<dir>/usr/share/fonts/chinese</dir>
<!-- the following element will be removed in the future -->
<dir>~/.fonts</dir>
清除下内存中的字体缓存。fc-cache fc-list查看下已安装的字体库(没有fc命令的百度安装下),最后再重启一下项目服务即可
[root@n1 fonts]# fc-cache
[root@n1 fonts]# fc-list
/usr/share/fonts/chinese/cour.ttf: Courier New:style=Standaard,Regular
/usr/share/fonts/chinese/consolai.ttf: Consolas:style=Italic
/usr/share/fonts/chinese/msgothic.ttc: MS Gothic:style=Regular
/usr/share/fonts/chinese/monbaiti.ttf: Mongolian Baiti:style=Regular
/usr/share/fonts/chinese/FRABKIT.TTF: Franklin Gothic Book:style=Italic,Kursivoitu
/usr/share/fonts/chinese/Sitka.ttc: Sitka Small,Sitka:style=Regular,Small
/usr/share/fonts/chinese/LBRITE.TTF: Lucida Bright:style=Regular
/usr/share/fonts/chinese/georgiai.ttf: Georgia:style=Italic,Cursiva
/usr/share/fonts/chinese/constanb.ttf: Constantia:style=Bold
/usr/share/fonts/chinese/ANTQUABI.TTF: Book Antiqua:style=Bold Italic,Lihavoitu Kursivoi
/usr/share/fonts/chinese/LCALLIG.TTF: Lucida Calligraphy:style=Italic
/usr/share/fonts/chinese/impact.ttf: Impact:style=Regular,Standard
/usr/share/fonts/chinese/DejaVuSansMono_0.ttf: DejaVu Sans Mono:style=Book
/usr/share/fonts/chinese/CALIST.TTF: Calisto MT:style=Regular,Κανονικά
/usr/share/fonts/chinese/BSSYM7.TTF: Bookshelf Symbol 7:style=Regular
/usr/share/fonts/chinese/palabi.ttf: Palatino Linotype:style=Negreta cursiva,Bold Italic
/usr/share/fonts/chinese/LTYPEB.TTF: Lucida Sans Typewriter:style=Bold,fed
/usr/share/fonts/chinese/ntailu.ttf: Microsoft New Tai Lue:style=Regular
...省略....