Skip to content

Commit f8b4be3

Browse files
committed
Straight filename extraction for URI path (decoded and standard separators)
See gh-29486
1 parent abf3400 commit f8b4be3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

spring-core/src/main/java/org/springframework/core/io/UrlResource.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ public UrlResource(String path) throws MalformedURLException {
101101
// Equivalent without java.net.URL constructor - for building on JDK 20+
102102
/*
103103
try {
104-
this.uri = ResourceUtils.toURI(StringUtils.cleanPath(path));
104+
String cleanedPath = StringUtils.cleanPath(path);
105+
this.uri = ResourceUtils.toURI(cleanedPath);
105106
this.url = this.uri.toURL();
106-
this.cleanedUrl = StringUtils.cleanPath(path);
107+
this.cleanedUrl = cleanedPath;
107108
}
108109
catch (URISyntaxException | IllegalArgumentException ex) {
109110
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
@@ -319,9 +320,14 @@ protected URL createRelativeURL(String relativePath) throws MalformedURLExceptio
319320
@Override
320321
@Nullable
321322
public String getFilename() {
322-
String originalPath = (this.uri != null ? this.uri.getPath() : this.url.getPath());
323-
String filename = StringUtils.getFilename(StringUtils.cleanPath(originalPath));
324-
return (filename != null ? URLDecoder.decode(filename, StandardCharsets.UTF_8) : null);
323+
if (this.uri != null) {
324+
// URI path is decoded and has standard separators
325+
return StringUtils.getFilename(this.uri.getPath());
326+
}
327+
else {
328+
String filename = StringUtils.getFilename(StringUtils.cleanPath(this.url.getPath()));
329+
return (filename != null ? URLDecoder.decode(filename, StandardCharsets.UTF_8) : null);
330+
}
325331
}
326332

327333
/**

0 commit comments

Comments
 (0)