yaoyi před 1 týdnem
rodič
revize
8cef5dc87e

+ 35 - 8
src/main/java/com/hr/repository/service/impl/FontManager.java

@@ -7,8 +7,10 @@ import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.ResourceLoader;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StreamUtils;
 
 import java.awt.*;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -61,19 +63,44 @@ public class FontManager {
 //            return new Font("SansSerif", Font.PLAIN, 12);
 //        }
         try {
-            Resource resource = resourceLoader.getResource("classpath:fonts/simsun.ttf");
+            String fontPath = "fonts/simsun.ttf";
+            Resource resource = resourceLoader.getResource("classpath:" + fontPath);
+
+            log.info("尝试加载字体文件: {}", fontPath);
+            log.info("资源存在: {}", resource.exists());
+            log.info("资源URL: {}", resource.getURL());
+
             if (!resource.exists()) {
-                log.error("字体文件不存在");
+                log.error("字体文件不存在: {}", fontPath);
+                return new Font("SansSerif", Font.PLAIN, 12);
+            }
+
+            // 获取文件大小
+            long contentLength = resource.contentLength();
+            log.info("字体文件大小: {} bytes", contentLength);
+
+            if (contentLength == 0) {
+                log.error("字体文件为空");
                 return new Font("SansSerif", Font.PLAIN, 12);
             }
 
-            InputStream fontStream = resource.getInputStream();
-            Font f = Font.createFont(Font.TRUETYPE_FONT, fontStream);
-            fontStream.close(); // 及时关闭
-            log.info("字体加载成功");
-            return f;
+            // 读取为字节数组并验证
+            byte[] fontData = StreamUtils.copyToByteArray(resource.getInputStream());
+            log.info("实际读取字节数: {}", fontData.length);
+
+            if (fontData.length == 0) {
+                log.error("无法读取字体数据");
+                return new Font("SansSerif", Font.PLAIN, 12);
+            }
+
+            // 创建字体
+            Font font = Font.createFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(fontData));
+            log.info("字体加载成功: {} (family: {}, name: {})",
+                    font.getFontName(), font.getFamily(), font.getName());
+            return font;
+
         } catch (Exception e) {
-            log.error("加载字体失败: {}", e.getMessage(), e);
+            log.error("加载字体失败", e);
             return new Font("SansSerif", Font.PLAIN, 12);
         }