这是在项目中需要调用其他项目的接口,其他项目需要证书才能访问,这个问题是在获取证书的时候遇到的。
假设证书文件位于项目的位置是:resources/config/tls/zhengshu.p12
通过以下代码获取证书位置,既可以使项目代码在idea下运行时获取到证书位置,也可以把项目打成jar或war包后运行代码也能获取到证书的位置。
try {
ClassPathResource classPathResource = new ClassPathResource("config/tls/zhengshu.p12");
// 通过流的形式获取到文件
InputStream inputStream = classPathResource.getInputStream();
} catch(Exception e) {
e.printStackTrace();
}
上面通过流的形式获取到了文件,如果需要目标文件,那么可以将流转化生成文件,至于具体操作可以百度。
如果没有看明白,也可以看一下参考文章,相信你肯定会解决这个问题。
参考文章:
https://stackoverflow.com/questions/25869428/classpath-resource-not-found-when-running-as-jar
https://blog.csdn.net/hero272285642/article/details/85119778?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight