一、准备资源文件
二、代码
import org.springframework.core.io.ClassPathResource;
import com.google.common.io.Resources;
private Font getFont(){
try {
Font actionJson = Font.createFont(Font.TRUETYPE_FONT,new ClassPathResource("classpath:font/SourceHanSansCN-Normal.otf").getInputStream());
Font actionJsonBase = actionJson.deriveFont(Font.BOLD,30);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(actionJsonBase);
return actionJsonBase;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
三、相关依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>