相关疑难解决方法(0)

从JAR获取文件

我正在使用Spring的Resource抽象来处理文件系统中的资源(文件).其中一个资源是JAR文件中的文件.根据以下代码,似乎引用有效

ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

// The path to the resource from the root of the JAR file
Resource fileInJar = resourcePatternResolver.getResources("/META-INF/foo/file.txt");

templateResource.exists(); // returns true
templateResource.isReadable();  // returns true
Run Code Online (Sandbox Code Playgroud)

在这一点上,一切都很好,但是当我尝试将其转换Resource为a时File

templateResource.getFile();
Run Code Online (Sandbox Code Playgroud)

我得到了例外

java.io.FileNotFoundException: class path resource [META-INF/foo/file.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/m2repo/uic-3.2.6-0.jar!/META-INF/foo/file.txt        
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:198)
at org.springframework.core.io.ClassPathResource.getFile(ClassPathResource.java:174)
Run Code Online (Sandbox Code Playgroud)

获取FileResourceJAR文件中存在的引用的正确方法是什么?

java io resources spring

12
推荐指数
2
解决办法
1万
查看次数

标签 统计

io ×1

java ×1

resources ×1

spring ×1