关于生成文件乱码或者小方块问题(Docker)

1.文件字符编码不对

        在后台创建文件的时候可能没有设置字符编码 ,所已在本地好使,线上不好使,特别微服务项目,容器安装字体等很费事,每次部署还会重置容器,重新安装。

// 转换为Reader对象,并指定编码格式为UTF-8
Reader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
// 关闭输入流
reader.close();

验证文件编码

 public static void main(String[] args) throws Exception {

        File file = new File("C:\\Users\\ZHOULIJIA\\Desktop\\SAD.pdf");


        String encoding = detectEncoding(file);
        if (encoding.isEmpty()) {
            System.out.println("无法判断文件编码格式");
        } else {
            System.out.println("文件编码格式:" + encoding);
        }

}

 public static String detectEncoding(File file) {
        try (InputStream inputStream = new FileInputStream(file)) {
            byte[] bom = new byte[4];
            int bytesRead = inputStream.read(bom);
            if (bytesRead >= 3 && bom[0] == (byte)0xEF && bom[1] == (byte)0xBB && bom[2] == (byte)0xBF) {
                return "UTF-8";
            } else if (bytesRead >= 2 && bom[0] == (byte)0xFF && bom[1] == (byte)0xFE) {
                return "UTF-16LE";
            } else if (bytesRead >= 2 && bom[0] == (byte)0xFE && bom[1] == (byte)0xFF) {
                return "UTF-16BE";
            } else if (bytesRead >= 4 && bom[0] == (byte)0x00 && bom[1] == (byte)0x00 && bom[2] == (byte)0xFE && bom[3] == (byte)0xFF) {
                return "UTF-32BE";
            } else if (bytesRead >= 4 && bom[0] == (byte)0xFF && bom[1] == (byte)0xFE && bom[2] == (byte)0x00 && bom[3] == (byte)0x00) {
                return "UTF-32LE";
            } else {
                // 默认返回UTF-8编码格式
                return "UTF-8";
            }
        } catch (IOException e) {
            e.printStackTrace();
            // 返回空字符串表示判断失败
            return "";
        }
    }

2.环境缺失字体

        将压缩包放置服务器下  然后copy到容器里

        /usr/share/fonts  将该压缩包解压   安装刷新缓存 不行就重启服务

手动(每次发布容器会重置)

复制文件到容器字体目录      docker cp /usr/ZHOUfonts.zip  容器id:/usr/share/fonts

进入容器      docker exec -it 容器id  /bin/bash 

解压     unzip ZHOUfonts.zip 

刷新字体缓存       fc-cache

退出 exit

DockerFile 自动

文件需要放置项目内
ADD ZHOUfonts.zip /usr/share/fonts/   复制
RUN test -f /usr/share/fonts/ZHOUfonts.zip   && echo "ZHOUfonts.zip exists"   || echo "ZHOUfonts.zip does not exist"  判断是否存在
RUN apt-get update && apt-get install -y unzip && unzip /usr/share/fonts  /ZHOUfonts.zip && fc-cache -fv   安装unzip  解压
RUN fc-cache -fv    刷新缓存

 缺点:繁琐 、打包慢

 

直接将文件包放在项目中

COPY /fonts/*  /usr/share/fonts/
RUN fc-cache

  较解压简洁快捷

  

3.容器环境编码不对

修改DockerFile

ENV LANG C.UTF-8

压缩包  (windos基本字体全包含)

http://链接: https://pan.baidu.com/s/1R-1mjSFWsNaUoxAoaYBvJw?pwd=8kkf 提取码: 8kkf 复制这段内容后打开百度网盘手机App,操作更方便哦

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT周小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值