Linux环境下"Font font = new Font("黑体", Font.BOLD, 18)"[BufferedImage~Graphics~drawString]中文乱码

Linux环境下"Font font = new Font("黑体", Font.BOLD, 18)"[BufferedImage~Graphics~drawString]中文乱码,windows下不会乱码,但在linux下nginx和tomcat部署后出现乱码、方框、问号等等,不能认清楚!!!


先说说系统:

Microsoft Windows [版本 10.0.10586]
(c) 2015 Microsoft Corporation。保留所有权利。

----------------------------------------------------------------------

CentOS release 6.5 (Final)
Kernel \r on an \m

----------------------------------------------------tomcat 8.0.28

好了,说怎么解决的吧步骤:

1:查看CentOS下”/usr/share/fonts“目录,查看新的系统则为空目录;无目录可手动创建fonts。

2:打开”C:\Windows\WinSxS\amd64_microsoft-windows-font-truetype-simsunb_31bf3856ad364e35_10.0.10586.0_none_1d80224ec6470747“等目录下,找到自己需要的字体的目录,那个文件夹也是见名知意了;

3:之后把挑选的字体,可以建个文件夹,一次传到Xftp等上传到服务器的”/usr/share/fonts“的目录下;

4:进入"cd /usr/share/fonts/windowsFonts"文件夹,注:windowsFonts为我挑选字体目录:

注:如果没有fonts目录可以手动创建,“windowsFonts”为自定义文件夹名称。

有楷体,黑体,简宋体,微软雅黑等,最后附上!

5:最后在此目录下,执行"#fc-cache -fv",扫描字体目录并生成字体信息的缓存,然后应用程序就可以"立即"使用这些新安装的字体;

6:最终还是要重启web sever,例如:tomcat,字体生效!

注:在ubuntu中需要安装:apt-get install fontconfig,侯执行以下命令!

执行命令 fc-cache -fv

[root@timespacexstar windowsFonts]# fc-cache -fv
/usr/share/fonts: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/windowsFonts: caching, new cache contents: 8 fonts, 0 dirs
/usr/share/X11/fonts/Type1: skipping, no such directory
/usr/share/X11/fonts/TTF: skipping, no such directory
/usr/local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/var/cache/fontconfig: cleaning cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded

root@iZ234iuy39uZ:/usr/share/fonts/windowsFonts# fc-cache -fv
/usr/share/fonts: caching, new cache contents: 0 fonts, 2 dirs
/usr/share/fonts/truetype: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/truetype/dejavu: caching, new cache contents: 6 fonts, 0 dirs
/usr/share/fonts/windowsFonts: caching, new cache contents: 8 fonts, 0 dirs
/usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/usr/share/fonts: caching, new cache contents: 0 fonts, 2 dirs
/usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/var/cache/fontconfig: cleaning cache directory
/root/.cache/fontconfig: not cleaning non-existent cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded

包涵字体:

[root@timespacexstar windowsFonts]# ll
total 77796
-rw-r--r-- 1 root root 16299048 Jan 31 17:43 msyhbd.ttc
-rw-r--r-- 1 root root 23608184 Jan 31 17:43 msyh.ttc
-rw-r--r-- 1 root root  9751720 Jan 31 17:45 simhei.ttf
-rw-r--r-- 1 root root 11785184 Jan 31 17:46 simkai.ttf
-rw-r--r-- 1 root root 18209004 Jan 31 17:47 simsun.ttc

windowsFonts字体

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
优化这段代码能产生不同的单元格宽度:public void myGraphicsGeneration(String name,String cellsValue[][], String path) { // 字体大小 int fontTitileSize = 15; // 横线的行数 int totalrow = cellsValue.length+1; // 竖线的行数 int totalcol = 0; if (cellsValue[0] != null) { totalcol = cellsValue[0].length; } // 图片宽度 int imageWidth = 1024; // 行高 int rowheight = 40; // 图片高度 int imageHeight = totalrow*rowheight+50; // 起始高度 int startHeight = 10; // 起始宽度 int startWidth = 10; // 单元格宽度 int colwidth = (int)((imageWidth-20)/totalcol); BufferedImage image = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0,0, imageWidth, imageHeight); graphics.setColor(new Color(220,240,240)); //画横线 for(int j=0;j<totalrow; j++){ graphics.setColor(Color.black); graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, startWidth + colwidth * totalcol, startHeight + (j + 1) * rowheight); } //画竖线 for(int k=0;k<totalcol+1;k++){ graphics.setColor(Color.black); graphics.drawLine(startWidth + k * colwidth, startHeight + rowheight, startWidth + k * colwidth, startHeight + rowheight * totalrow); } //设置字体 Font font = new Font("微软雅黑",Font.BOLD,fontTitileSize); graphics.setFont(font); //写标题 String title = "【"+ name +"】"; graphics.drawString(title, startWidth, startHeight+rowheight-10); //写入内容 for(int n=0;n<cellsValue.length;n++){ for(int l=0;l<cellsValue[n].length;l++){ if (n == 0) { font = new Font("微软雅黑",Font.BOLD,fontTitileSize); graphics.setFont(font); }else if (n > 0 && l >0) { font = new Font("微软雅黑",Font.PLAIN,fontTitileSize); graphics.setFont(font); graphics.setColor(Color.RED); } else { font = new Font("微软雅黑",Font.PLAIN,fontTitileSize); graphics.setFont(font); graphics.setColor(Color.BLACK); } if(cellsValue[n][l]==null){ cellsValue[n][l] = ""; } graphics.drawString(cellsValue[n][l].toString(), startWidth + colwidth * l + 5, startHeight + rowheight * (n + 2) - 10); } } // 保存图片 createImage(image, path); }
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值