Skip to content

Commit 0e22c3d

Browse files
committed
Convert file: URL to FileSystemResource for backward compatibility
See spring-projectsgh-29163
1 parent 508be4e commit 0e22c3d

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.lang.reflect.Method;
2727
import java.net.JarURLConnection;
2828
import java.net.MalformedURLException;
29+
import java.net.URI;
2930
import java.net.URISyntaxException;
3031
import java.net.URL;
3132
import java.net.URLClassLoader;
@@ -765,9 +766,9 @@ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource
765766
files.forEach(file -> {
766767
if (getPathMatcher().match(patternPath.toString(), file.toString())) {
767768
try {
768-
result.add(new UrlResource(file.toUri()));
769+
result.add(convertToResource(file.toUri()));
769770
}
770-
catch (MalformedURLException ex) {
771+
catch (Exception ex) {
771772
// ignore
772773
}
773774
}
@@ -849,14 +850,12 @@ protected Set<Resource> findAllModulePathResources(String locationPattern) throw
849850
}
850851

851852
@Nullable
852-
private static Resource findResource(ModuleReader moduleReader, String name) {
853+
private Resource findResource(ModuleReader moduleReader, String name) {
853854
try {
854855
return moduleReader.find(name)
855856
// If it's a "file:" URI, use FileSystemResource to avoid duplicates
856857
// for the same path discovered via class-path scanning.
857-
.map(uri -> ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme()) ?
858-
new FileSystemResource(uri.getPath()) :
859-
UrlResource.from(uri))
858+
.map(this::convertToResource)
860859
.orElse(null);
861860
}
862861
catch (Exception ex) {
@@ -867,6 +866,12 @@ private static Resource findResource(ModuleReader moduleReader, String name) {
867866
}
868867
}
869868

869+
private Resource convertToResource(URI uri) {
870+
return ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme()) ?
871+
new FileSystemResource(uri.getPath()) :
872+
UrlResource.from(uri);
873+
}
874+
870875
private static String stripLeadingSlash(String path) {
871876
return (path.startsWith("/") ? path.substring(1) : path);
872877
}

0 commit comments

Comments
 (0)