从 jar 文件中读取 .cer 文件失败

Reading .cer files from jar file failing(从 jar 文件中读取 .cer 文件失败)
本文介绍了从 jar 文件中读取 .cer 文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在运行时安装客户端 SSL 证书.证书已经用 jar 打包,可在以下位置获得

I was trying to install client SSl cert at runtime. The certs are already packed with jar and is available in the below location

尝试使用以下代码从上述路径读取所有可用的 .cer 文件时,失败了

While trying to read all available .cer files from the above mentioned path using below code ,it got failed

 String rootPath = this.getClass().getClassLoader().getResource("certs/"+activeProfile+"/").getPath();
 File folder = new File(rootPath);
 File[] listOfFiles = folder.listFiles();

在调试时,我得到了rootPath";作为文件:/workspace/mnb-123-no-data-flow-trigger-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes!/certs/dev/".但是文件列表(listOfFiles)为空.

while debugging I am getting "rootPath" as "file:/workspace/mnb-123-no-data-flow-trigger-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes!/certs/dev/" .But the list of files(listOfFiles ) is null.

但是使用下面的代码,我可以获取rootPath"中的一个文件的内容

But using the below code I am able to get the content of one of the file in "rootPath"

InputStream in = this.getClass().getResourceAsStream("/certs/"+activeProfile+"/server.cer");

            File tempFile= File.createTempFile("temporary",".cer");
            tempFile.deleteOnExit();
            try (FileOutputStream out = new FileOutputStream(tempFile)) {
                IOUtils.copy(in, out);
            }
            displayFile(tempFile);

第一个片段中的错误是什么?如何从路径中读取所有文件?

What is the error in the first snippet? how can I read all files from the path?

推荐答案

正如 Luke 在评论中指出的那样,Java File 只处理操作系统文件系统上的真实"文件(和目录),在 Java7 增加了新 I/O";(NIO) 被称为默认"文件系统,但我们现在可以拥有其他 Java 定义的文件系统,包括对 jar/zip 中的条目起作用的文件系统:

As Luke indicates in comment, Java File only handles 'real' files (and directories) on the OS filesystem, which in Java 7 up with "new I/O" (NIO) is called the 'default' filesystem, but we can now have other Java-defined filesystems including one that does work on entries in a jar/zip:

    String name = ...;
    URI uri = (myclass/loader).getResource(name).toURI(); 
    // I think Paths.get using ZipFileSystemProvider ought to handle the whole URI but it doesn't, so:
    String[] spec = uri.getSchemeSpecificPart().split("!/");
    if( ! uri.getScheme().equals("jar") || spec.length != 2 || ! spec[0].startsWith("file:") ) throw new Exception ("bad URI for jar resource");
    FileSystem fs = FileSystems.newFileSystem(new URI("jar",spec[0],null), new HashMap<String,Object>());
    Files.list(fs.getPath(spec[1])) .forEach(p -> System.out.println(p.toString()));
    // instead of System.out.println can do something else, or instead of .forEach .collect into a variable
    fs.close(); // or use try-resources to close automatically

这篇关于从 jar 文件中读取 .cer 文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)