Skip to content

Commit 10cb2cc

Browse files
committed
Avoid triggering lazy resolution in MultipartResolver.cleanupMultipart
Issue: SPR-16640
1 parent 24aae2e commit 10cb2cc

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -33,6 +33,7 @@
3333
import org.springframework.web.multipart.MultipartException;
3434
import org.springframework.web.multipart.MultipartHttpServletRequest;
3535
import org.springframework.web.multipart.MultipartResolver;
36+
import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;
3637
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
3738
import org.springframework.web.util.WebUtils;
3839

@@ -188,11 +189,14 @@ protected String determineEncoding(HttpServletRequest request) {
188189

189190
@Override
190191
public void cleanupMultipart(MultipartHttpServletRequest request) {
191-
try {
192-
cleanupFileItems(request.getMultiFileMap());
193-
}
194-
catch (Throwable ex) {
195-
logger.warn("Failed to perform multipart cleanup for servlet request", ex);
192+
if (!(request instanceof AbstractMultipartHttpServletRequest) ||
193+
((AbstractMultipartHttpServletRequest) request).isResolved()) {
194+
try {
195+
cleanupFileItems(request.getMultiFileMap());
196+
}
197+
catch (Throwable ex) {
198+
logger.warn("Failed to perform multipart cleanup for servlet request", ex);
199+
}
196200
}
197201
}
198202

spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -108,6 +108,18 @@ public MultiValueMap<String, MultipartFile> getMultiFileMap() {
108108
return getMultipartFiles();
109109
}
110110

111+
/**
112+
* Determine whether the underlying multipart request has been resolved.
113+
* @return {@code true} when eagerly initialized or lazily triggered,
114+
* {@code false} in case of a lazy-resolution request that got aborted
115+
* before any parameters or multipart files have been accessed
116+
* @since 4.3.15
117+
* @see #getMultipartFiles()
118+
*/
119+
public boolean isResolved() {
120+
return (this.multipartFiles != null);
121+
}
122+
111123

112124
/**
113125
* Set a Map with parameter names as keys and list of MultipartFile objects as values.

spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -52,6 +52,7 @@
5252
* @author Juergen Hoeller
5353
* @author Rossen Stoyanchev
5454
* @since 3.1
55+
* @see StandardServletMultipartResolver
5556
*/
5657
public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest {
5758

@@ -75,9 +76,10 @@ public StandardMultipartHttpServletRequest(HttpServletRequest request) throws Mu
7576
* @param lazyParsing whether multipart parsing should be triggered lazily on
7677
* first access of multipart files or parameters
7778
* @throws MultipartException if an immediate parsing attempt failed
79+
* @since 3.2.9
7880
*/
79-
public StandardMultipartHttpServletRequest(HttpServletRequest request,
80-
boolean lazyParsing) throws MultipartException {
81+
public StandardMultipartHttpServletRequest(HttpServletRequest request, boolean lazyParsing)
82+
throws MultipartException {
8183

8284
super(request);
8385
if (!lazyParsing) {

spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class StandardServletMultipartResolver implements MultipartResolver {
7171
* corresponding exceptions at the time of the {@link #resolveMultipart} call.
7272
* Switch this to "true" for lazy multipart parsing, throwing parse exceptions
7373
* once the application attempts to obtain multipart files or parameters.
74+
* @since 3.2.9
7475
*/
7576
public void setResolveLazily(boolean resolveLazily) {
7677
this.resolveLazily = resolveLazily;
@@ -94,17 +95,20 @@ public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request)
9495

9596
@Override
9697
public void cleanupMultipart(MultipartHttpServletRequest request) {
97-
// To be on the safe side: explicitly delete the parts,
98-
// but only actual file parts (for Resin compatibility)
99-
try {
100-
for (Part part : request.getParts()) {
101-
if (request.getFile(part.getName()) != null) {
102-
part.delete();
98+
if (!(request instanceof AbstractMultipartHttpServletRequest) ||
99+
((AbstractMultipartHttpServletRequest) request).isResolved()) {
100+
// To be on the safe side: explicitly delete the parts,
101+
// but only actual file parts (for Resin compatibility)
102+
try {
103+
for (Part part : request.getParts()) {
104+
if (request.getFile(part.getName()) != null) {
105+
part.delete();
106+
}
103107
}
104108
}
105-
}
106-
catch (Throwable ex) {
107-
LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex);
109+
catch (Throwable ex) {
110+
LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex);
111+
}
108112
}
109113
}
110114

0 commit comments

Comments
 (0)