|
|
@@ -2,7 +2,10 @@ package com.hr.repository.service.impl;
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.core.io.ResourceLoader;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.awt.*;
|
|
|
@@ -17,7 +20,8 @@ import java.io.InputStream;
|
|
|
@Component
|
|
|
@Slf4j
|
|
|
public class FontManager {
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private ResourceLoader resourceLoader;
|
|
|
private volatile Font cachedFont;
|
|
|
private final Object lock = new Object();
|
|
|
|
|
|
@@ -46,14 +50,30 @@ public class FontManager {
|
|
|
*/
|
|
|
private Font loadBaseFont() {
|
|
|
|
|
|
- ClassPathResource resource = new ClassPathResource("fonts/simsun.ttf");
|
|
|
+// ClassPathResource resource = new ClassPathResource("fonts/simsun.ttf");
|
|
|
+// try {
|
|
|
+// InputStream fontStream = resource.getInputStream();
|
|
|
+// Font f= Font.createFont(Font.TRUETYPE_FONT, fontStream);
|
|
|
+// log.info("字体加载成功");
|
|
|
+// return f;
|
|
|
+// }catch (Exception e) {
|
|
|
+// log.error("加载字体失败:{}",e.getMessage(),e);
|
|
|
+// return new Font("SansSerif", Font.PLAIN, 12);
|
|
|
+// }
|
|
|
try {
|
|
|
+ Resource resource = resourceLoader.getResource("classpath:fonts/simsun.ttf");
|
|
|
+ if (!resource.exists()) {
|
|
|
+ log.error("字体文件不存在");
|
|
|
+ return new Font("SansSerif", Font.PLAIN, 12);
|
|
|
+ }
|
|
|
+
|
|
|
InputStream fontStream = resource.getInputStream();
|
|
|
- Font f= Font.createFont(Font.TRUETYPE_FONT, fontStream);
|
|
|
+ Font f = Font.createFont(Font.TRUETYPE_FONT, fontStream);
|
|
|
+ fontStream.close(); // 及时关闭
|
|
|
log.info("字体加载成功");
|
|
|
return f;
|
|
|
- }catch (Exception e) {
|
|
|
- log.error("加载字体失败:{}",e.getMessage(),e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("加载字体失败: {}", e.getMessage(), e);
|
|
|
return new Font("SansSerif", Font.PLAIN, 12);
|
|
|
}
|
|
|
|