【问题标题】:getResource on OpenJdk throws java.nio.file.NoSuchFileExceptionOpenJdk 上的 getResource 抛出 java.nio.file.NoSuchFileException
【发布时间】:2020-09-16 05:02:47
【问题描述】:

应用程序:Spring Boot,
图片:OpenJDK 8,
PaaS:Openshift,
包装:罐子,
错误:没有这样的文件错误。 java.nio.file.NoSuchFileException
文件位置:/servicename/src/main/resources/cert/trust.cer
错误位置:String certs = readFile((trustCertPath).getPath(), Charset.defaultCharset());
更新 1 后的错误位置:try (InputStream is = classloader.getResourceAsStream(resourcePath)) 为空。

我使用 cert/trust.cer 作为路径。由于它不起作用,我尝试使用 /cert/trust.cer、trust.cer、./cert/trust.cer。我能够使用 Windows (/cert/trust.cer) 在本地运行此类,但从命令行失败,并且在部署时也失败了。

更新 1:我正在使用 getResourceAsStream(cert/trust.cer)。 Inputstream 的结果为 null。

public class CertsUtility implements InitializingBean {
public static final Logger logger = LoggerFactory.getLogger("CertsUtility");
private String keystorePaaS;
private String keystorePass;
private String CertPath = "cert/trust.cer";

public void setKeystorePaaS(String keystorePaaS) {
    this.keystorePaaS = keystorePaaS;
}

public void setKeystorePass(String keystorePass) {
    this.keystorePass = keystorePass;
}

static File getPath(String path) {
    URL url = CertsUtility.class.getClass().getResource(path);
    if (url != null) {
        File file = new File(url.getPath());
        return file;
    } else {
        File file = new File(path);
        return file;
    }
}

static String getResourceFileAsString(String resourcePath) throws IOException {
    ClassLoader classloader = ClassLoader.getSystemClassLoader();
    try (InputStream is = classloader.getResourceAsStream(resourcePath)) {
        if (is == null)
            return null;
        try (InputStreamReader isr = new InputStreamReader(is)) {
            BufferedReader reader = new BufferedReader(isr);
            String targetString = reader.lines().collect(Collectors.joining());
            return targetString;
        }
    }

}

void genIndividualandLoad() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException,
        CertificateException, IOException, InterruptedException {
    try {
        KeyStore keyStore = KeyStore.getInstance("JKS");
        InputStream fileInputStream = new FileInputStream(keystorePaaS);
        keyStore.load(fileInputStream, keystorePass.toCharArray());
        fileInputStream.close();
        String certs = readFile((trustCertPath).getPath(), Charset.defaultCharset());
        String[] certificates = certs.split("(?<=-----END CERTIFICATE-----\n)");

        for (int i = 0; i < certificates.length - 1; i++) {
            String individualName = getPath(CertPath).getParent() + i + ".cer";
            try (FileOutputStream outputStream = new FileOutputStream(individualName)) {
                byte[] strToBytes = certificates[i].getBytes();
                outputStream.write(strToBytes);
                try (InputStream inStream = new FileInputStream(individualName)) {
                    CertificateFactory cf = CertificateFactory.getInstance("X.509");
                    X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
                    keyStore.setCertificateEntry("" + i, cert);
                }
                FileOutputStream fileOutputStream = new FileOutputStream(getPath(keystorePaaS));
                keyStore.store(fileOutputStream, keystorePass.toCharArray());
                fileOutputStream.close();
                outputStream.close();
            }
        }
    } catch (KeyStoreException e) {
        logger.error("| genIndividualandLoad() | Keystore exception occurred", e);
    } catch (FileNotFoundException e) {
        logger.error("| genIndividualandLoad() | File not found exception occurred", e);
    } catch (NoSuchAlgorithmException e) {
        logger.error("| genIndividualandLoad() | Algorithm related exception occurred", e);
    } catch (CertificateException e) {
        logger.error("| genIndividualandLoad() | X.509 Certificate exception occurred", e);
    } catch (IOException e) {
        logger.error("| genIndividualandLoad() | I/O exception occured", e);
    }
}

public void afterPropertiesSet() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException,
        CertificateException, IOException, InterruptedException {
    genIndividualandLoad();
}

}

【问题讨论】:

  • 证书实际位于磁盘的什么位置?你可以运行类似find / -name 'trust.cer' 的命令来找到它吗?
  • @omajid:无法访问 Pod 上的终端。
  • 你能在java中实现find吗?在您的应用程序中有什么东西遍历所有以/ 开头的文件以找到trust.cert 并打印出路径?
  • 呃,您的证书在 cert/trust.cer 中(来自类路径根目录),并且您正在尝试访问 trust.cer,但没有提及 cert/ 文件夹。你确定这是正确的吗?
  • 让我编辑它。首先,当我尝试解决问题时,我使用的是 cert/trust.cer,但它不适用于 getResource。

标签: java null getresource


【解决方案1】:

您正在尝试将证书作为文件加载,如果它实际上是文件系统中的文件(即在您的本地工作区中),这将起作用,但当证书嵌套在您的 JAR 文件中时则不会。

你需要做类似的事情

getClass().getClassLoader().getResourceAsStream("cert/trust.cer");

然后从结果流中读取。

【讨论】:

【解决方案2】:

我使用了来自 org.springframework.core.io.ClassPathResource 的 ClassPathResource 来解决这个问题。

【讨论】:

    猜你喜欢
    • 2016-07-29
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2014-02-17
    相关资源
    最近更新 更多