OpenJdk 上的 getResource 抛出 java.nio.file.NoSuchFileException
getResource on OpenJdk throws java.nio.file.NoSuchFileException
应用程序:Spring启动,
图片: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)) is null.
我使用了 cert/trust.cer 作为路径。由于它不起作用,我尝试使用 /cert/trust.cer、trust.cer、./cert/trust.cer。我能够在本地使用 Windows (/cert/trust.cer) 运行 这个 class 但是从命令行失败并且在明显部署时也失败。
更新 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();
}
}
您正在尝试将证书作为文件加载,如果它实际上是您的文件系统中的一个文件(即在您的本地工作区中),那么它将起作用,但当证书嵌套在您的 JAR 文件中时则不起作用。
相反,您需要做一些类似于
的事情
getClass().getClassLoader().getResourceAsStream("cert/trust.cer");
然后从结果流中读取。
我使用了 org.springframework.core.io.ClassPathResource 中的 ClassPathResource 来解决这个问题。
应用程序:Spring启动,
图片: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)) is null.
我使用了 cert/trust.cer 作为路径。由于它不起作用,我尝试使用 /cert/trust.cer、trust.cer、./cert/trust.cer。我能够在本地使用 Windows (/cert/trust.cer) 运行 这个 class 但是从命令行失败并且在明显部署时也失败。
更新 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();
}
}
您正在尝试将证书作为文件加载,如果它实际上是您的文件系统中的一个文件(即在您的本地工作区中),那么它将起作用,但当证书嵌套在您的 JAR 文件中时则不起作用。
相反,您需要做一些类似于
的事情getClass().getClassLoader().getResourceAsStream("cert/trust.cer");
然后从结果流中读取。
我使用了 org.springframework.core.io.ClassPathResource 中的 ClassPathResource 来解决这个问题。