简体   繁体   中英

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?

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

To load all files in a folder (eg /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/**);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM