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基本字体全包含)