从Spring Boot的Fat JAR中读取目录

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

Read directory from Spring Boot's Fat JAR

问题

我正尝试在我的Spring Boot 2应用程序中读取resources/configs目录。以下是该目录结构的示例:

  1. resources
  2. | configs
  3. | | file1.xml
  4. | | file2.xml
  5. | | ...

我正在读取目录和嵌套文件的代码如下:

  1. @Value("${app.configs-path}")
  2. String configsPath;
  3. ...
  4. Resource configsDirectoryResource = new ClassPathResource(configsPath);
  5. InputStream configsDirectoryStream = configsDirectoryResource.getInputStream();
  6. InputStreamReader configsDirectoryStreamReader = new InputStreamReader(configsDirectoryStream);
  7. BufferedReader configsDirectoryBufferedReader = new BufferedReader(configsDirectoryStreamReader);
  8. List<String> configFileNames = configsDirectoryBufferedReader.lines().collect(toList());

还有我的application.properties配置文件内容如下:

  1. 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:

  1. resources
  2. | configs
  3. | | file1.xml
  4. | | file2.xml
  5. | | ...

Code where I'm reading the directory and the nested files:

  1. @Value(&quot;${app.configs-path}&quot;)
  2. String configsPath;
  3. ...
  4. Resource configsDirectoryResource = new ClassPathResource(configsPath);
  5. InputStream configsDirectoryStream = configsDirectoryResource.getInputStream();
  6. InputStreamReader configsDirectoryStreamReader = new InputStreamReader(configsDirectoryStream);
  7. BufferedReader configsDirectoryBufferedReader = new BufferedReader(configsDirectoryStreamReader);
  8. List&lt;String&gt; configFileNames = configsDirectoryBufferedReader.lines().collect(toList());

And my application.properties:

  1. 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

示例代码:

  1. ClassLoader cl = this.getClass().getClassLoader();
  2. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
  3. Resource[] resources = resolver.getResources("classpath*:/"+configsPath+"/*.xml");
  4. for (Resource resource: resources){
  5. logger.info(resource.getFilename());
  6. }
英文:

You can use PathMatchingResourcePatternResolver from Spring Framework.

Example code:

  1. ClassLoader cl = this.getClass().getClassLoader();
  2. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
  3. Resource[] resources = resolver.getResources(&quot;classpath*:/&quot;+configsPath+&quot;/*.xml&quot;) ;
  4. for (Resource resource: resources){
  5. logger.info(resource.getFilename());
  6. }

答案2

得分: 0

好的,那比我想象的要简单。

  1. ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
  2. Resource[] resources = resourcePatternResolver.getResources(configsPath + "/*.xml");

然后,我们可以从每个 resource 中获取一个 InputStream 并对它们进行操作。

英文:

OK, that was easier than I thought.

  1. ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
  2. 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:

确定