KeyStore keyStore = KeyStore.getInstance("JKS");
Obect is = BaiYongExternalImpl.class.getClassLoader().getResourceAsStream("tomcat.keystore");
//获取证书文件转为输入流成功
keystore.load((InputStream) is,password.toCharArray());
//keystore.load()产生java.io.Exception?
//求助?
可否发出一个示例方法 供研究?
从方法来看有可能是InputStream在调用load方法前 已经关闭
/*
* 获取证书文件
* @param password
* */
public static KeyStore getKeyStore(String password, String keyStorePath) throws Exception {
Object is = null;
KeyStore ks = null;
File file;
try {
try {
String keyStoPath = System.getProperty("keyStorePath");
if (StringUtils.isEmpty(keyStoPath)) {
throw new Exception("jvm参数中未配置信任文件路径,开始获取jar包内部证书");
}
file = new File(keyStoPath);
if (!file.isFile()) {
throw new Exception("jvm参数中配置的信任文件路径错误");
}
if (file.getName().endsWith(".keystore")) {
throw new Exception("jvm启动参数配置的信任文件不是keystore类型");
}
is = new FileInputStream(file);
ks = KeyStore.getInstance("JKS");
log.info("jvm启动参数配置的信任文件加载成功");
} catch (Exception e) {
try {
ks = KeyStore.getInstance("JKS");
//获取文件tomcat.keystore并转换为输入流
is = SSLPostUtils.class.getClassLoader().getResourceAsStream("tomcat.keystore");
if (is == null) {
throw new Exception("获取jar包内部证书文件失败");
}
ks.load((InputStream) is, password.toCharArray());
log.info("获取jar包内部证书文件成功!!!");
} catch (Exception e1) {
log.error("从jvm启动参数,内置证书文件中获取信任文件均失败:{}", e1);
}
}
} catch (Exception var1) {
try {
file = new File(keyStorePath);
is = new FileInputStream(file);
ks = KeyStore.getInstance("JKS");
ks.load((InputStream) is, password.toCharArray());
log.info("从指定路径去证书文件成功");
} catch (Exception var2) {
throw new Exception("从指定路径中获取信任文件失败");
}
} finally {
try {
if (is != null) {
((InputStream) is).close();
}
} catch (IOException var1) {
}
}
return ks;
}