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.
21
21
22
22
import org .apache .commons .logging .LogFactory ;
23
23
24
+ import org .springframework .http .MediaType ;
24
25
import org .springframework .util .StringUtils ;
25
26
import org .springframework .web .multipart .MultipartException ;
26
27
import org .springframework .web .multipart .MultipartHttpServletRequest ;
32
33
* To be added as "multipartResolver" bean to a Spring DispatcherServlet context,
33
34
* without any extra configuration at the bean level (see below).
34
35
*
36
+ * <p>This resolver variant uses your Servlet container's multipart parser as-is,
37
+ * potentially exposing the application to container implementation differences.
38
+ * See {@link org.springframework.web.multipart.commons.CommonsMultipartResolver}
39
+ * for an alternative implementation using a local Commons FileUpload library
40
+ * within the application, providing maximum portability across Servlet containers.
41
+ * Also, see this resolver's configuration option for
42
+ * {@link #setStrictServletCompliance strict Servlet compliance}, narrowing the
43
+ * applicability of Spring's {@link MultipartHttpServletRequest} to form data only.
44
+ *
35
45
* <p><b>Note:</b> In order to use Servlet 3.0 based multipart parsing,
36
46
* you need to mark the affected servlet with a "multipart-config" section in
37
47
* {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement}
55
65
* @author Juergen Hoeller
56
66
* @since 3.1
57
67
* @see #setResolveLazily
68
+ * @see #setStrictServletCompliance
58
69
* @see HttpServletRequest#getParts()
59
70
* @see org.springframework.web.multipart.commons.CommonsMultipartResolver
60
71
*/
61
72
public class StandardServletMultipartResolver implements MultipartResolver {
62
73
63
74
private boolean resolveLazily = false ;
64
75
76
+ private boolean strictServletCompliance = false ;
77
+
65
78
66
79
/**
67
80
* Set whether to resolve the multipart request lazily at the time of
@@ -76,10 +89,32 @@ public void setResolveLazily(boolean resolveLazily) {
76
89
this .resolveLazily = resolveLazily ;
77
90
}
78
91
92
+ /**
93
+ * Specify whether this resolver should strictly comply with the Servlet
94
+ * specification, only kicking in for "multipart/form-data" requests.
95
+ * <p>Default is "false", trying to process any request with a "multipart/"
96
+ * content type as far as the underlying Servlet container supports it
97
+ * (which works on e.g. Tomcat but not on Jetty). For consistent portability
98
+ * and in particular for consistent custom handling of non-form multipart
99
+ * request types outside of Spring's {@link MultipartResolver} mechanism,
100
+ * switch this flag to "true": Only "multipart/form-data" requests will be
101
+ * wrapped with a {@link MultipartHttpServletRequest} then; other kinds of
102
+ * requests will be left as-is, allowing for custom processing in user code.
103
+ * <p>Note that Commons FileUpload and therefore
104
+ * {@link org.springframework.web.multipart.commons.CommonsMultipartResolver}
105
+ * supports any "multipart/" request type. However, it restricts processing
106
+ * to POST requests which standard Servlet multipart parsers might not do.
107
+ * @since 5.3.9
108
+ */
109
+ public void setStrictServletCompliance (boolean strictServletCompliance ) {
110
+ this .strictServletCompliance = strictServletCompliance ;
111
+ }
112
+
79
113
80
114
@ Override
81
115
public boolean isMultipart (HttpServletRequest request ) {
82
- return StringUtils .startsWithIgnoreCase (request .getContentType (), "multipart/" );
116
+ return StringUtils .startsWithIgnoreCase (request .getContentType (),
117
+ (this .strictServletCompliance ? MediaType .MULTIPART_FORM_DATA_VALUE : "multipart/" ));
83
118
}
84
119
85
120
@ Override
0 commit comments