【发布时间】:2010-08-10 08:39:42
【问题描述】:
我正在使用 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
此时,一切都很好,但是当我尝试将 Resource 转换为 File 时
templateResource.getFile();
我得到了异常
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)
获取 File 对存在于 JAR 文件中的 Resource 的引用的正确方法是什么?
【问题讨论】: