Skip to content

Commit df49b11

Browse files
committed
CommonsMultipartFile removes mixed separator paths from original filename
Issue: SPR-13662 (cherry picked from commit 5d9d88c)
1 parent d1f5ee2 commit df49b11

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.springframework.web.multipart.MultipartFile;
3232

3333
/**
34-
* MultipartFile implementation for Apache Commons FileUpload.
34+
* {@link MultipartFile} implementation for Apache Commons FileUpload.
3535
*
3636
* @author Trevor D. Cook
3737
* @author Juergen Hoeller
@@ -78,12 +78,13 @@ public String getOriginalFilename() {
7878
// Should never happen.
7979
return "";
8080
}
81+
8182
// Check for Unix-style path
82-
int pos = filename.lastIndexOf("/");
83-
if (pos == -1) {
84-
// Check for Windows-style path
85-
pos = filename.lastIndexOf("\\");
86-
}
83+
int unixSep = filename.lastIndexOf("/");
84+
// Check for Windows-style path
85+
int winSep = filename.lastIndexOf("\\");
86+
// Cut off at latest possible point
87+
int pos = (winSep > unixSep ? winSep : unixSep);
8788
if (pos != -1) {
8889
// Any sort of path separator found...
8990
return filename.substring(pos + 1);

spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -111,6 +111,8 @@ private void doTestWithApplicationContext(boolean lazy) throws Exception {
111111
doTestFiles(request);
112112

113113
doTestBinding(resolver, originalRequest, request);
114+
115+
wac.close();
114116
}
115117

116118
private void doTestParameters(MultipartHttpServletRequest request) {
@@ -381,9 +383,9 @@ public List<FileItem> parseRequest(HttpServletRequest request) {
381383
MockFileItem fileItem1x = new MockFileItem(
382384
"field1", "type1", empty ? "" : "field1.txt", empty ? "" : "text1");
383385
MockFileItem fileItem2 = new MockFileItem(
384-
"field2", "type2", empty ? "" : "C:/field2.txt", empty ? "" : "text2");
386+
"field2", "type2", empty ? "" : "C:\\mypath/field2.txt", empty ? "" : "text2");
385387
MockFileItem fileItem2x = new MockFileItem(
386-
"field2x", "type2", empty ? "" : "C:\\field2x.txt", empty ? "" : "text2");
388+
"field2x", "type2", empty ? "" : "C:/mypath\\field2x.txt", empty ? "" : "text2");
387389
MockFileItem fileItem3 = new MockFileItem("field3", null, null, "value3");
388390
MockFileItem fileItem4 = new MockFileItem("field4", "text/html; charset=iso-8859-1", null, "value4");
389391
MockFileItem fileItem5 = new MockFileItem("field4", null, null, "value5");

0 commit comments

Comments
 (0)