Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the all files from specified path using spring 'ResourcePatternResolver'

I need to get the all files irrespective for its file name using spring ResourcePatternResolver.

I have already tried with following code

private static final String BPMN_PATH = "process";
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resource = resourcePatternResolver.getResources("classpath:" + BPMN_PATH + "**/*.bpmn");

But this is only if the file lists are in the classpath(project directory).

In my scenario the files are located in system directory. For this I have tried with following code

private static final String BPMN_PATH = System.getProperty("user.home");
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resource = resourcePatternResolver.getResources("file:" + BPMN_PATH + File.separator +"process.bpmn");

But this will work only for the specified file (process.bpmn).

Can anyone please help on this?

like image 722
Noyal Avatar asked Sep 06 '25 02:09

Noyal


2 Answers

Well I remember the ant-style wildcard pattern should also work for file:.

To load all files in a folder (e.g. /foo/bar/) , you could use :

    resourcePatternResolver.getResources("file:/foo/bar/*");

Please note that it is only limited to the files included in this folder . It will not include the file inside its sub-folders.

If you want to recursively load all files , even the files in each sub-folders and sub-sub-folder and etc., you could use:

     resourcePatternResolver.getResources("file:/foo/bar/**");
like image 139
Ken Chan Avatar answered Sep 08 '25 18:09

Ken Chan


Check the Spring Framework PathMatchingResourcePatternResolver.

  1. /WEB-INF/*-context.xml
  2. com/mycompany/**/applicationContext.xml
  3. file:C:/some/path/*-context.xml
  4. classpath:com/mycompany/**/applicationContext.xml

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/support/PathMatchingResourcePatternResolver.html

like image 20
Alptekin T. Avatar answered Sep 08 '25 17:09

Alptekin T.