Noyal
Noyal

Reputation: 253

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?

Upvotes: 4

Views: 11606

Answers (2)

Alptekin T.
Alptekin T.

Reputation: 115

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

Upvotes: 1

Ken Chan
Ken Chan

Reputation: 90497

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/**");

Upvotes: 1

Related Questions