gpt4 book ai didi

java - 创建资源路径时从 ZipFileSystemProvider 获取 FileSystemNotFoundException

转载 作者:IT老高 更新时间:2023-10-28 21:05:10 27 4
gpt4 key购买 nike

我有一个 Maven 项目,在一个方法中,我想在我的资源文件夹中创建一个目录的路径。这样做是这样的:

try {
final URI uri = getClass().getResource("/my-folder").toURI();
Path myFolderPath = Paths.get(uri);
} catch (final URISyntaxException e) {
...
}

生成的 URI 看起来像 jar:file:/C:/path/to/my/project.jar!/my-folder

堆栈跟踪如下:

Exception in thread "pool-4-thread-1" java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Paths.java:143)

URI 似乎是有效的。 ! 之前的部分指向生成的 jar 文件,之后的部分指向存档根目录中的 my-folder。我之前使用过这个说明来创建我的资源的路径。为什么我现在遇到异常?

最佳答案

您需要先创建文件系统,然后才能访问 zip 中的路径,例如

final URI uri = getClass().getResource("/my-folder").toURI();
Map<String, String> env = new HashMap<>();
env.put("create", "true");
FileSystem zipfs = FileSystems.newFileSystem(uri, env);
Path myFolderPath = Paths.get(uri);

这不是自动完成的。

http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html

关于java - 创建资源路径时从 ZipFileSystemProvider 获取 FileSystemNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25032716/

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