1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .web .multipart .commons ;
18
18
19
+ import java .util .Arrays ;
20
+ import java .util .HashSet ;
19
21
import java .util .List ;
22
+ import java .util .Set ;
20
23
21
24
import javax .servlet .ServletContext ;
22
25
import javax .servlet .http .HttpServletRequest ;
27
30
import org .apache .commons .fileupload .FileUploadBase ;
28
31
import org .apache .commons .fileupload .FileUploadException ;
29
32
import org .apache .commons .fileupload .servlet .ServletFileUpload ;
33
+ import org .apache .commons .fileupload .servlet .ServletRequestContext ;
30
34
35
+ import org .springframework .lang .Nullable ;
31
36
import org .springframework .util .Assert ;
32
37
import org .springframework .web .context .ServletContextAware ;
33
38
import org .springframework .web .multipart .MaxUploadSizeExceededException ;
41
46
/**
42
47
* Servlet-based {@link MultipartResolver} implementation for
43
48
* <a href="https://commons.apache.org/proper/commons-fileupload">Apache Commons FileUpload</a>
44
- * 1.2 or above.
49
+ * 1.2 or above. This resolver variant delegates to a local FileUpload library
50
+ * within the application, providing maximum portability across Servlet containers.
51
+ *
52
+ * <p>Commons FileUpload traditionally parses POST requests with any "multipart/" type.
53
+ * Supported HTTP methods may be customized through {@link #setSupportedMethods}.
45
54
*
46
55
* <p>Provides "maxUploadSize", "maxInMemorySize" and "defaultEncoding" settings as
47
56
* bean properties (inherited from {@link CommonsFileUploadSupport}). See corresponding
52
61
* Needs to be initialized <i>either</i> by an application context <i>or</i>
53
62
* via the constructor that takes a ServletContext (for standalone usage).
54
63
*
64
+ * <p>Note: The common alternative is
65
+ * {@link org.springframework.web.multipart.support.StandardServletMultipartResolver},
66
+ * delegating to the Servlet container's own multipart parser, with configuration to
67
+ * happen at the container level and potentially with container-specific limitations.
68
+ *
55
69
* @author Trevor D. Cook
56
70
* @author Juergen Hoeller
57
71
* @since 29.09.2003
58
72
* @see #CommonsMultipartResolver(ServletContext)
59
73
* @see #setResolveLazily
74
+ * @see #setSupportedMethods
60
75
* @see org.apache.commons.fileupload.servlet.ServletFileUpload
61
76
* @see org.apache.commons.fileupload.disk.DiskFileItemFactory
77
+ * @see org.springframework.web.multipart.support.StandardServletMultipartResolver
62
78
*/
63
79
public class CommonsMultipartResolver extends CommonsFileUploadSupport
64
80
implements MultipartResolver , ServletContextAware {
65
81
66
82
private boolean resolveLazily = false ;
67
83
84
+ @ Nullable
85
+ private Set <String > supportedMethods ;
86
+
68
87
69
88
/**
70
89
* Constructor for use as bean. Determines the servlet container's
@@ -101,6 +120,17 @@ public void setResolveLazily(boolean resolveLazily) {
101
120
this .resolveLazily = resolveLazily ;
102
121
}
103
122
123
+ /**
124
+ * Specify supported methods as an array of HTTP method names.
125
+ * The traditional Commons FileUpload default is "POST" only.
126
+ * <p>When configured as a Spring property value,
127
+ * this can be a comma-separated String: e.g. "POST,PUT".
128
+ * @since 5.3.9
129
+ */
130
+ public void setSupportedMethods (String ... supportedMethods ) {
131
+ this .supportedMethods = new HashSet <>(Arrays .asList (supportedMethods ));
132
+ }
133
+
104
134
/**
105
135
* Initialize the underlying {@code org.apache.commons.fileupload.servlet.ServletFileUpload}
106
136
* instance. Can be overridden to use a custom subclass, e.g. for testing purposes.
@@ -122,7 +152,10 @@ public void setServletContext(ServletContext servletContext) {
122
152
123
153
@ Override
124
154
public boolean isMultipart (HttpServletRequest request ) {
125
- return ServletFileUpload .isMultipartContent (request );
155
+ return (this .supportedMethods != null ?
156
+ this .supportedMethods .contains (request .getMethod ()) &&
157
+ FileUploadBase .isMultipartContent (new ServletRequestContext (request )) :
158
+ ServletFileUpload .isMultipartContent (request ));
126
159
}
127
160
128
161
@ Override
0 commit comments