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{


    @PostConstruct
    public void fontSet() {
        try {
            log.info("fontSet 进入");
            OsInfo os = new OsInfo();
            // 判断是不是 linux
            if (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安装宋体字体库,生成的图片就不会出现乱码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot Flowable UI 是一个基于Spring Boot的流程管理和业务流程设计的用户界面。它是Flowable流程引擎的一部分,提供了可视化的界面来管理和设计流程。 要在SpringBoot项目中使用Flowable UI,你需要按照以下步骤进行配置: 1. 下载Flowable UI的war包,并将其中的静态资源文件拷贝至SpringBoot项目的对应文件夹下(一般是`resources/static`)。 2. 在SpringBoot项目的`application.yml`文件中进行Flowable相关的配置,包括数据库的连接信息、数据表的创建和更新规则等。 这样,你就可以在SpringBoot项目中使用Flowable UI来管理和设计流程了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Spring Boot 整合 Flowable-ui-modeler 6.7.2代码](https://download.csdn.net/download/wangdaoyin2010/85146346)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot集成Flowable UI](https://blog.csdn.net/feiqinbushizheng/article/details/108284971)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值