I am trying to get a properties file from within a zip file. I need to use a wild card, because I will want to match either "my.properties" or "my_en.properties". I create a ResourcePatternResolver like so:
ClassLoader loader = MyClass.class.getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(loader);
I succeed when I try to retrieve the "my.properties" file using no wild card:
resolver.getResource("file:C:/somePath/a.zip/META-INF/my.properties").exists()
returns true. However, if I add a wild card to the file name, it fails, e.g.,
resolver.getResource("file:C:/somePath/a.zip/META-INF/my*.properties").exists()
returns false. What can I do to match and retrieve either file? I am trying to do this in a webapp within Tomcat.
The documentation is unclear about this, but the getResource method does not use PathMatcher internally to resolve the resource (that means that no wildcard is allowed), try getResources(String locationPattern) instead.
For example :
Resource[] resources = resolver.getResources("file:C:/somePath/a.zip/META-INF/my*.properties");
for(Resource resource : resources) {
// do something for each resource found
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With