老项目在本地启动正常,但是打成jar启动失败cannot be resolved to absolute file path because it does not reside in the file system
Caused by: java.io.FileNotFoundException: class path resource [文件路径] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/E:/全路径/target/ramp-hub-0.0.1.jar!/BOOT-INF/classes!//xxxxxxx.jks
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:180)
原代码:
HttpClient client = null;
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(ResourceUtils.getFile(storeFile),
PWD.toCharArray(), FilePWD.toCharArray())
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
分析发现是读取不到jar里面的文件,应换成读取文件流
//临时文件
String storeFile = classpath:xxxx/xxxx.jks
File targetFile = new File("src/main/resources/targetFile.tmp");
InputStream resourceAsStream = null;
try {
//文件流形式
resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(storeFile);
if (null != resourceAsStream) {
FileUtils.copyInputStreamToFile(resourceAsStream, targetFile);
} else {
throw new Exception(storeFile + " 的文件不存在");
}
} catch (Exception e) {
logger.error("获取文件失败:", e);
throw e;
} finally {
try {
if(null != resourceAsStream){
resourceAsStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// HttpClient
HttpClient client = null;
// 创建SSL上下文
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(targetFile,PWD.toCharArray(), FilePWD.toCharArray())
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
重新打包启动成功!