记录-Springboot项目打包后无法读取resource目录下文件的解决方法(不使用文件流)

604 阅读1分钟

记录-Springboot项目打包后无法读取resource目录下文件的解决方法

项目需要生成pdf文件,引入了两个字体文件,本地运行时没问题,打包上传至服务器后报异常如下。

src/main/resources/static/FZXBSJW.TTF not found as file or resource.

项目中文件引入方式:

String path = "classpath:src/main/resources/static/FZXBSJW.TTF";
BaseFont bf =BaseFont.createFont(path , BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bf,30,Font.NORMAL);

修改后:

ClassPathResource classPathResource1 = new ClassPathResource("static/FZXBSJW.TTF");
BaseFont bf =BaseFont.createFont(classPathResource1.getPath(), BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bf,30,Font.NORMAL);