开发环境正常运行jar包时出现not reside in the file system找不到文件

老项目在本地启动正常,但是打成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();

重新打包启动成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值