ソースを参照

加载字体3

yaoyi 1 週間 前
コミット
3dcd2b94fe
1 ファイル変更22 行追加21 行削除
  1. 22 21
      src/main/java/com/hr/repository/service/impl/FontManager.java

+ 22 - 21
src/main/java/com/hr/repository/service/impl/FontManager.java

@@ -65,46 +65,47 @@ public class FontManager {
 //            return new Font("SansSerif", Font.PLAIN, 12);
 //        }
         try {
-            String fontPath = "fonts/simsun.ttf";
-            Resource resource = resourceLoader.getResource("classpath:" + fontPath);
-
-            log.info("尝试加载字体文件: {}", fontPath);
+            ClassPathResource resource = new ClassPathResource("fonts/simsun.ttf");
+            log.info("尝试加载字体文件: {}", resource.getPath());
             log.info("资源存在: {}", resource.exists());
 
             if (!resource.exists()) {
-                log.error("字体文件不存在: {}", fontPath);
+                log.error("字体文件不存在: fonts/simsun.ttf");
                 return new Font("SansSerif", Font.PLAIN, 12);
             }
 
-            // 直接读取为字节数组
-            byte[] fontData = StreamUtils.copyToByteArray(resource.getInputStream());
-            log.info("字体文件大小: {} bytes", fontData.length);
+            try (InputStream is = resource.getInputStream()) {
+                if (is == null) {
+                    log.error("字体文件输入流为 null");
+                    return new Font("SansSerif", Font.PLAIN, 12);
+                }
 
-            if (fontData.length == 0) {
-                log.error("字体文件为空");
-                return new Font("SansSerif", Font.PLAIN, 12);
-            }
+                byte[] fontData = StreamUtils.copyToByteArray(is);
+                log.info("字体文件大小: {} bytes", fontData.length);
 
-            // 直接从字节数组创建字体,无需临时文件
-            Font font = Font.createFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(fontData));
-            Font derivedFont = font.deriveFont(12f); // 设置字体大小
+                if (fontData.length == 0) {
+                    log.error("字体文件为空");
+                    return new Font("SansSerif", Font.PLAIN, 12);
+                }
+
+                Font font = Font.createFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(fontData));
+                Font derivedFont = font.deriveFont(12f);
 
-            log.info("字体加载成功: {} (family: {}, name: {})",
-                    derivedFont.getFontName(), derivedFont.getFamily(), derivedFont.getName());
+                log.info("字体加载成功: {} (family: {}, name: {})",
+                        derivedFont.getFontName(), derivedFont.getFamily(), derivedFont.getName());
 
-            return derivedFont;
+                return derivedFont;
 
+            }
         } catch (Exception e) {
             log.error("加载字体失败", e);
-            // 提供多种备选方案
             try {
-                // 尝试使用系统默认字体
                 Font defaultFont = new Font("Dialog", Font.PLAIN, 12);
                 log.info("使用默认字体: {}", defaultFont.getName());
                 return defaultFont;
             } catch (Exception ex) {
                 log.error("创建默认字体也失败", ex);
-                return Font.decode(null); // 返回系统默认字体
+                return Font.decode(null);
             }
         }