Skip to content

Commit b2b1ff6

Browse files
committed
CommonsMultipartResolver cleans up all multipart files in case of multiple files for same name as well (SPR-2784)
1 parent 255d1ad commit b2b1ff6

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/multipart/CommonsPortletMultipartResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -179,7 +179,7 @@ protected String determineEncoding(ActionRequest request) {
179179
public void cleanupMultipart(MultipartActionRequest request) {
180180
if (request != null) {
181181
try {
182-
cleanupFileItems(request.getFileMap().values());
182+
cleanupFileItems(request.getMultiFileMap());
183183
}
184184
catch (Throwable ex) {
185185
logger.warn("Failed to perform multipart cleanup for portlet request", ex);

org.springframework.web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -18,11 +18,10 @@
1818

1919
import java.io.IOException;
2020
import java.io.UnsupportedEncodingException;
21-
import java.util.Collection;
21+
import java.nio.charset.Charset;
2222
import java.util.HashMap;
2323
import java.util.List;
2424
import java.util.Map;
25-
import java.nio.charset.Charset;
2625

2726
import org.apache.commons.fileupload.FileItem;
2827
import org.apache.commons.fileupload.FileItemFactory;
@@ -32,12 +31,12 @@
3231
import org.apache.commons.logging.LogFactory;
3332

3433
import org.springframework.core.io.Resource;
34+
import org.springframework.http.MediaType;
3535
import org.springframework.util.LinkedMultiValueMap;
3636
import org.springframework.util.MultiValueMap;
3737
import org.springframework.util.StringUtils;
3838
import org.springframework.web.multipart.MultipartFile;
3939
import org.springframework.web.util.WebUtils;
40-
import org.springframework.http.MediaType;
4140

4241
/**
4342
* Base class for multipart resolvers that use Jakarta Commons FileUpload
@@ -271,14 +270,16 @@ protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String
271270
* @param multipartFiles Collection of MultipartFile instances
272271
* @see org.apache.commons.fileupload.FileItem#delete()
273272
*/
274-
protected void cleanupFileItems(Collection<MultipartFile> multipartFiles) {
275-
for (MultipartFile file : multipartFiles) {
276-
if (file instanceof CommonsMultipartFile) {
277-
CommonsMultipartFile cmf = (CommonsMultipartFile) file;
278-
cmf.getFileItem().delete();
279-
if (logger.isDebugEnabled()) {
280-
logger.debug("Cleaning up multipart file [" + cmf.getName() + "] with original filename [" +
281-
cmf.getOriginalFilename() + "], stored " + cmf.getStorageDescription());
273+
protected void cleanupFileItems(MultiValueMap<String, MultipartFile> multipartFiles) {
274+
for (List<MultipartFile> files : multipartFiles.values()) {
275+
for (MultipartFile file : files) {
276+
if (file instanceof CommonsMultipartFile) {
277+
CommonsMultipartFile cmf = (CommonsMultipartFile) file;
278+
cmf.getFileItem().delete();
279+
if (logger.isDebugEnabled()) {
280+
logger.debug("Cleaning up multipart file [" + cmf.getName() + "] with original filename [" +
281+
cmf.getOriginalFilename() + "], stored " + cmf.getStorageDescription());
282+
}
282283
}
283284
}
284285
}

org.springframework.web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -20,11 +20,11 @@
2020
import javax.servlet.ServletContext;
2121
import javax.servlet.http.HttpServletRequest;
2222

23+
import org.apache.commons.fileupload.FileItem;
2324
import org.apache.commons.fileupload.FileItemFactory;
2425
import org.apache.commons.fileupload.FileUpload;
2526
import org.apache.commons.fileupload.FileUploadBase;
2627
import org.apache.commons.fileupload.FileUploadException;
27-
import org.apache.commons.fileupload.FileItem;
2828
import org.apache.commons.fileupload.servlet.ServletFileUpload;
2929

3030
import org.springframework.util.Assert;
@@ -184,7 +184,7 @@ protected String determineEncoding(HttpServletRequest request) {
184184
public void cleanupMultipart(MultipartHttpServletRequest request) {
185185
if (request != null) {
186186
try {
187-
cleanupFileItems(request.getFileMap().values());
187+
cleanupFileItems(request.getMultiFileMap());
188188
}
189189
catch (Throwable ex) {
190190
logger.warn("Failed to perform multipart cleanup for servlet request", ex);

0 commit comments

Comments
 (0)