jar包内的类无法访问jar包内的ca证书的问题

在Java开发中,将ca证书文件打包进jar后,遇到'no such file or dictionary'异常。通过分析,发现使用绝对路径无法正确访问。解决办法是采用流方式读取jar包内资源,构建SSLContext对象。参考了相关资料,深入理解了jar内类访问资源的路径问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

在开发过程中使用绝对路径访问ca证书文件,结果打成jar包后,发生了 no such file or dictionary 的异常

解决方法

使用

String certPath = this.getClass().getResource("/${name}").getPath();

java中jar包内的类访问jar包内部的资源文件的路径问题
来获取路径时,得到的路径是

!BOOT-INF/Classes!/${name}

仍然会造成找不到文件的问题
出现!BOOT-INF/Classes!/${name}路径的解释
以流(stream)的方式读取资源,然后用于创建SSLContext 对象,才解决

public SSLContext doCustom(InputStream ins,String keyStorepass){
    	SSLContext sc = null;
        KeyStore trustStore = null;
        try {
            trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
 
            trustStore.load(ins, keyStorepass.toCharArray());
             sc = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
        } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | KeyManagementException e) {
            e.printStackTrace();
        } finally {
            try {
            	ins.close();
            } catch (IOException e) {
            }
        }
        return sc;
    }
public String doPostJsonObj(String url, String json){
    	KeyStore trustStore = null;
    	InputStream certStream = this.getClass().getResourceAsStream("/${certName}");
    	HttpSSLClientUtil httpsClient = new HttpSSLClientUtil();

  	SSLContext sslcontext = httpsClient.doCustom(certStream, "${pwd}");
  	SSLConnectionSocketFactory sslcsf = new SSLConnectionSocketFactory(sslcontext,
              SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  	
   Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
              .register("http", PlainConnectionSocketFactory.INSTANCE)
              .register("https", new SSLConnectionSocketFactory(sslcontext))
              .build();
      PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
      HttpClients.custom().setConnectionManager(connManager);

      CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslcsf).build();
      
      CloseableHttpResponse response=null;
      String resultString = "";
      try {
     
          HttpPost httpPost = new HttpPost(url);
          StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
          httpPost.setEntity(entity);
          response = httpClient.execute(httpPost);
          resultString = EntityUtils.toString(response.getEntity(), "utf-8");
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
          try {
              response.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }

      return resultString;
    	
    }

相关参考资料

从jar包中读取文件
jar读取内外的资源配置
jar内的类获取jar包内文件资源的分析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值