4

我正在尝试检索类路径中的文件列表,但 Spring 向我返回了 FileSystemResources 的 Resource[] 而不是 ClassPathResources。代码如下所示:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource [] resources = resolver.getResources("classpath*:my/package/**/*.xml");

我也试过这个:

context.getResources("classpath*:my/package/**/*.xml");

返回的数组是一堆绝对路径 file:// URL,而我真正想要的是 classpath: URL。我究竟做错了什么?

这就是我希望最终构建的内容:

/my/package/one.xml
/my/package/two.xml

相反,我得到了这个:

file:/C:/eclipse/.../my/package/one.xml
4

1 回答 1

0

FileSystemResource 和 ClassPathResource 都返回绝对资源路径。FileSystemResource 在文件系统中寻找它C:/eclipse/.../my/package/one.xml。ClassPathResource 在内部的类路径文件中查找资源/WEB-INF/classes。我不明白你为什么需要 return classpath:path,但是你仍然可以通过子串你的绝对路径来得到它。

于 2012-05-24T17:12:25.730 回答