英文:
Read directory from Spring Boot's Fat JAR
问题
我正尝试在我的Spring Boot 2应用程序中读取resources/configs
目录。以下是该目录结构的示例:
resources
| configs
| | file1.xml
| | file2.xml
| | ...
我正在读取目录和嵌套文件的代码如下:
@Value("${app.configs-path}")
String configsPath;
...
Resource configsDirectoryResource = new ClassPathResource(configsPath);
InputStream configsDirectoryStream = configsDirectoryResource.getInputStream();
InputStreamReader configsDirectoryStreamReader = new InputStreamReader(configsDirectoryStream);
BufferedReader configsDirectoryBufferedReader = new BufferedReader(configsDirectoryStreamReader);
List<String> configFileNames = configsDirectoryBufferedReader.lines().collect(toList());
还有我的application.properties
配置文件内容如下:
app.configs-path=configs
当我在IDE中运行应用程序时,这个方法运行得很好。configsDirectoryResource
对象返回文件名的流,然后我逐个读取嵌套文件。但是当我运行JAR文件时,configsDirectoryResource
对象不返回任何文件名。然而,我可以直接读取XML文件,但我希望能够在运行时获得文件列表。
我该如何在JAR文件中读取目录或获取目录中的文件列表呢?
英文:
I'm trying to read the resources/configs
directory in my Spring Boot 2 application. Here is the example of the structure of the directory:
resources
| configs
| | file1.xml
| | file2.xml
| | ...
Code where I'm reading the directory and the nested files:
@Value("${app.configs-path}")
String configsPath;
...
Resource configsDirectoryResource = new ClassPathResource(configsPath);
InputStream configsDirectoryStream = configsDirectoryResource.getInputStream();
InputStreamReader configsDirectoryStreamReader = new InputStreamReader(configsDirectoryStream);
BufferedReader configsDirectoryBufferedReader = new BufferedReader(configsDirectoryStreamReader);
List<String> configFileNames = configsDirectoryBufferedReader.lines().collect(toList());
And my application.properties
:
app.configs-path=configs
When I run my application in IDE, this works fine. The object configsDirectoryResource
returns stream of filenames, and then I read every nested file. But when I launch the JAR file, configsDirectoryResource
returns no filenames. However I can read XML files directly, but I'd like to get list of files in runtime.
How can I read the directory or get list of files in the directory which is in the JAR file?
答案1
得分: 0
可以使用 Spring Framework 中的 PathMatchingResourcePatternResolver
。
示例代码:
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("classpath*:/"+configsPath+"/*.xml");
for (Resource resource: resources){
logger.info(resource.getFilename());
}
英文:
You can use PathMatchingResourcePatternResolver
from Spring Framework.
Example code:
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("classpath*:/"+configsPath+"/*.xml") ;
for (Resource resource: resources){
logger.info(resource.getFilename());
}
答案2
得分: 0
好的,那比我想象的要简单。
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resourcePatternResolver.getResources(configsPath + "/*.xml");
然后,我们可以从每个 resource
中获取一个 InputStream
并对它们进行操作。
英文:
OK, that was easier than I thought.
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resourcePatternResolver.getResources(configsPath + "/*.xml");
Then we can get an InputStream
from each resource
and work with them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论