使用 Java.io.file,每当我尝试从中读取证书时,它总是指向错误的路径

Working with Java.io.file, when ever I try to read a cert from it it always points to wrong path

每次我尝试在

下访问我的证书时,我都在尝试读取带有文件 IO 的证书
testApp/src/main/resources -> cert

它总是显示

X:\workspace1\testApp\target\classes\cert\test.p12

这是我正在使用的代码,它总是出现空指针异常。

// String fileName = "config/sample.txt";
    ClassLoader classLoader = App2.class.getClassLoader();

    File file = new File(classLoader.getResource("cert/test.p12").getFile());
    FileInputStream fm = new FileInputStream(file);
    KeyStore ks = KeyStore.getInstance("PKCS12");
    try {
        ks.load(fm, "test".toCharArray());
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    Key key = ks.getKey("test", "test".toCharArray());
    Certificate cert = ks.getCertificate("test");
    PublicKey publicKey = cert.getPublicKey();
    System.out.println("Public key");
    System.out.println(Base64.getEncoder().encodeToString(publicKey.getEncoded()));
    fm.close();

我想读取此证书以提取 public 或私钥。

这对我有用

File file = new File(classLoader.getResource("./cert/test.p12").getFile());