读取来自JAR文件的.cer文件失败

huangapple go评论68阅读模式
英文:

Reading .cer files from jar file failing

问题

我试图在运行时安装客户端SSL证书。证书已经与jar包一起打包,并且位于以下位置:

读取来自JAR文件的.cer文件失败

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

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

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

但是使用以下代码,我能够获取"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);

第一段代码中有什么错误?我怎样才能从该路径读取所有文件?

英文:

I was trying to install client SSl cert at runtime. The certs are already packed with jar and is available in the below location
读取来自JAR文件的.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();

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.

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?

答案1

得分: 1

正如卢克在评论中指出的,Java的File仅处理操作系统文件系统上的'真实'文件(和目录),在Java 7及以上版本中,通过"新I/O"(NIO)称为'默认'文件系统,但现在我们可以拥有其他Java定义的文件系统,包括一个可以处理jar/zip中条目的文件系统:

		String name = ...;
		URI uri = (myclass/loader).getResource(name).toURI(); 
		// 我认为使用ZipFileSystemProvider的Paths.get应该可以处理整个URI,但事实并非如此:
		String[] spec = uri.getSchemeSpecificPart().split("!/");
		if (!uri.getScheme().equals("jar") || spec.length != 2 || !spec[0].startsWith("file:")) throw new Exception("jar资源的URI错误");
		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()));
		// 可以替换为其他操作,或者将.forEach替换为.collect以收集到变量中
		fs.close(); // 或者使用try-resources来自动关闭
英文:

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&#39;t, so:
	String[] spec = uri.getSchemeSpecificPart().split(&quot;!/&quot;);
	if( ! uri.getScheme().equals(&quot;jar&quot;) || spec.length != 2 || ! spec[0].startsWith(&quot;file:&quot;) ) throw new Exception (&quot;bad URI for jar resource&quot;);
	FileSystem fs = FileSystems.newFileSystem(new URI(&quot;jar&quot;,spec[0],null), new HashMap&lt;String,Object&gt;());
	Files.list(fs.getPath(spec[1])) .forEach(p -&gt; 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

huangapple
  • 本文由 发表于 2020年10月10日 01:24:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64284646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定