当前位置: 首页 > news >正文

springboot、flowable 生成图片发布到Docker乱码问题

flowable自带的方法生成图片时,如设置字体为宋体,则本地测试没有问题,因为windows自带宋体字体库,但是如果发布到Docker,则会出现乱码问题,因为大部分Docker并不包含宋体字体库;

通过Java代码,在项目启动前向Docker添加字体库

我自己使用的方法是在spring boot启动前,通过Java代码设置Docker字体,具体步骤如下:

1. 复制字体库到spring boot的resource内,如何获取字体库可以参考上面手动的步骤

2. 添加容器启动前执行的代码

package com.ruoyi.web.controller.tool;import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.system.OsInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;@Configuration
@ComponentScan
@Slf4j
public class BaseConfiguration{@PostConstructpublic void fontSet() {try {log.info("fontSet 进入");OsInfo os = new OsInfo();// 判断是不是 linuxif (os.isLinux()) {PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();Resource[] resources = pathMatchingResourcePatternResolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "**/fonts/**/*.ttc");log.info("有哪些文件 \n {}",resources);log.info("fontSet 复制文件开始");for (Resource resource : resources) {copy(resource);}log.info("fontSet 复制文件结束");String x = RuntimeUtil.execForStr("apt-get update");log.info("执行 apt-get update \n{}", x);// 安装字体管理插件String s = RuntimeUtil.execForStr("apt-get install fontconfig -y");log.info("执行 apt-get install fontconfig -y 结果 \n{}", s);// 使安装的字体生效String s2 = RuntimeUtil.execForStr("fc-cache -fv");log.info("执行 fc-cache -fv 结果 \n{}", s2);// 查看是否存在String s3 = RuntimeUtil.execForStr("fc-list");log.info("执行 fc-list结果 \n{}", s3);}} catch (IOException e) {log.error("fontSet 字体复制失败");}}public void copy(Resource resource) throws IOException {URL url = resource.getURL();String path = url.getPath();int i = path.lastIndexOf("/");String fileName = path.substring(i);log.info("fontSet fileName is:" + fileName);File file = new File("/usr/share/fonts" + fileName);if (!file.exists()) {File parentFile = file.getParentFile();if (!parentFile.exists()) {parentFile.mkdirs();}file.createNewFile();FileOutputStream o = new FileOutputStream(file);IoUtil.copy(resource.getInputStream(), o);}}}

注 : docker 没有yum 命令,使用apt-get 命令执行

 完成上述步骤那么在项目启动前,会在Docker安装宋体字体库,生成的图片就不会出现乱码


http://www.mrgr.cn/news/18782.html

相关文章:

  • AOP和注解的配合使用(封装通用日志处理类)
  • 团建活动如何分组?
  • LearnOpenGL学习笔记
  • 探索电子翻书在展厅的应用,如何进一步优化信息展示与接收?
  • 高校为什么需要AIGC大数据实验室?
  • 青远生态为云南林业规划院定制开发的自然保护地规划智能编制系统顺利通过验收
  • ubunto中常见问题及WSL
  • 盘古信息IMS V6:先进稳定的企业数字化转型方案
  • 王立铭脑科学50讲后续5,自己从课程中提起自己所需的知识,安放到自己的知识体系中。
  • 使用dx工具将jar和class打包成dex
  • Web3社交新经济,与 SOEX 实现无缝交易的高级安全性
  • 软件测试基础知识整理(详细版)
  • 2024精选:四大必备音乐剪辑神器推荐!
  • Android --- observer和observerForever的区别
  • 中国水资源用水紧张程度数据(栅格/0.5度)
  • ZBrush入门使用介绍——14、ZRemesher
  • 数理天地杂志数理天地杂志社数理天地编辑部2024年第12期目录
  • jupyter 安装新内核后报找不到已安装的包
  • Web Worker
  • 中国农村政策与改革统计年报(2015-2022年)