从Spring Boot的Fat JAR中读取目录

huangapple go评论65阅读模式
英文:

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(&quot;${app.configs-path}&quot;)
String configsPath;

...

Resource configsDirectoryResource = new ClassPathResource(configsPath);
InputStream configsDirectoryStream = configsDirectoryResource.getInputStream();
InputStreamReader configsDirectoryStreamReader = new InputStreamReader(configsDirectoryStream);
BufferedReader configsDirectoryBufferedReader = new BufferedReader(configsDirectoryStreamReader);

List&lt;String&gt; 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(&quot;classpath*:/&quot;+configsPath+&quot;/*.xml&quot;) ;
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 + &quot;/*.xml&quot;);

Then we can get an InputStream from each resource and work with them.

huangapple
  • 本文由 发表于 2020年9月2日 17:31:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63702706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定