0%

Springboot从resources读取文件

工作中需要把一个证书文件打包到jar包中供读取使用,目录结构如下:
2020-07-09-1.png
在本地(未打包)测试的时候读取没有问题,但在测试环境(打包后)报如下错误:

1
java.io.FileNotFoundException: class path resource [wep/test.p12] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/LL/wepay.jar!/BOOT-INF/classes!/wep/test.p12

代码如下:

1
2
3
final String resourceLocation = "classpath:wep/"+wepayConfig.getCertFile();
File file = ResourceUtils.getFile(resourceLocation);
FileInputStream fis = new FileInputStream(file);

换成下面的代码可以解决这个问题

1
2
ClassPathResource resource = new ClassPathResource("wep/"+wepayConfig.getCertFile());
InputStream fis = resource.getInputStream();