gpt4 book ai didi

java - 获取证书对象时出错

转载 作者:太空宇宙 更新时间:2023-11-04 08:12:16 24 4
gpt4 key购买 nike

import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import org.junit.Test;

public class TrustManagerTest {
@Test
public void testTrust() throws Exception
{
System.out.println("done 0");
Certificate cert = CertificateFactory.getInstance("X509").generateCertificate(getClass().getResourceAsStream("Resources/certificate.cer"));
System.out.println("done 1");
}
}

我已检查了certificate.cer 文件的内容,它确实包含证书。我在创建证书对象时遇到以下异常。知道我哪里出错了吗?

java.security.cert.CertificateException: Missing input stream
at sun.security.provider.X509Factory.engineGenerateCertificate(Unknown Source)
at java.security.cert.CertificateFactory.generateCertificate(Unknown Source)
at com.manh.msf.rest.auth.TrustManagerTest.testTrust(TrustManagerTest.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)

最佳答案

是的。

getResourceAsStream("Resources/certificate.cer")

如果/Resources的父目录不在您的CLASSPATH中,则getResourceAsStream将返回null

这对我来说效果很好:

public class ClassLoaderDemo {
public static void main(String[] args) {
try {
ClassLoader classLoader = ClassLoaderDemo.class.getClassLoader();
if (classLoader != null) {
InputStream is = classLoader.getResourceAsStream("mesh.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s = "";
while ((s = br.readLine()) != null) {
System.out.println(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

我的输入文件不在包中;我把它放在/resources 目录中,并将其添加到我的 CLASSPATH 中。

关于java - 获取证书对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10989075/

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