gpt4 book ai didi

java - OpenJdk 上的 getResource 抛出 java.nio.file.NoSuchFileException

转载 作者:行者123 更新时间:2023-12-01 16:29:49 25 4
gpt4 key购买 nike

应用程序:Spring Boot,
图片:OpenJDK 8,
PaaS:Openshift,
包装: jar ,
错误:没有这样的文件错误。 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)) is null。

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

更新 1:我正在使用 getResourceAsStream(cert/trust.cer)。输入流的结果为空。

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();
}

}

最佳答案

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

相反,你需要做类似的事情

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

然后从结果流中读取。

关于java - OpenJdk 上的 getResource 抛出 java.nio.file.NoSuchFileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62072188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com