From df49b1175810a495b23f671e4e592887569dee93 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 14 Nov 2015 23:16:13 +0100 Subject: [PATCH] CommonsMultipartFile removes mixed separator paths from original filename Issue: SPR-13662 (cherry picked from commit 5d9d88c) --- .../web/multipart/commons/CommonsMultipartFile.java | 13 +++++++------ .../commons/CommonsMultipartResolverTests.java | 8 +++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java index aea5769344f..eb0c3349865 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java @@ -31,7 +31,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.web.multipart.MultipartFile; /** - * MultipartFile implementation for Apache Commons FileUpload. + * {@link MultipartFile} implementation for Apache Commons FileUpload. * * @author Trevor D. Cook * @author Juergen Hoeller @@ -78,12 +78,13 @@ public class CommonsMultipartFile implements MultipartFile, Serializable { // Should never happen. return ""; } + // Check for Unix-style path - int pos = filename.lastIndexOf("/"); - if (pos == -1) { - // Check for Windows-style path - pos = filename.lastIndexOf("\\"); - } + int unixSep = filename.lastIndexOf("/"); + // Check for Windows-style path + int winSep = filename.lastIndexOf("\\"); + // Cut off at latest possible point + int pos = (winSep > unixSep ? winSep : unixSep); if (pos != -1) { // Any sort of path separator found... return filename.substring(pos + 1); diff --git a/spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java b/spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java index 8a9a12e0a42..e0c963df8dd 100644 --- a/spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -111,6 +111,8 @@ public class CommonsMultipartResolverTests { doTestFiles(request); doTestBinding(resolver, originalRequest, request); + + wac.close(); } private void doTestParameters(MultipartHttpServletRequest request) { @@ -381,9 +383,9 @@ public class CommonsMultipartResolverTests { MockFileItem fileItem1x = new MockFileItem( "field1", "type1", empty ? "" : "field1.txt", empty ? "" : "text1"); MockFileItem fileItem2 = new MockFileItem( - "field2", "type2", empty ? "" : "C:/field2.txt", empty ? "" : "text2"); + "field2", "type2", empty ? "" : "C:\\mypath/field2.txt", empty ? "" : "text2"); MockFileItem fileItem2x = new MockFileItem( - "field2x", "type2", empty ? "" : "C:\\field2x.txt", empty ? "" : "text2"); + "field2x", "type2", empty ? "" : "C:/mypath\\field2x.txt", empty ? "" : "text2"); MockFileItem fileItem3 = new MockFileItem("field3", null, null, "value3"); MockFileItem fileItem4 = new MockFileItem("field4", "text/html; charset=iso-8859-1", null, "value4"); MockFileItem fileItem5 = new MockFileItem("field4", null, null, "value5");