This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 6.0.26! |
Multipart Resolver
MultipartResolver
from the org.springframework.web.multipart
package is a strategy
for parsing multipart requests including file uploads. There is a container-based
StandardServletMultipartResolver
implementation for Servlet multipart request parsing.
Note that the outdated CommonsMultipartResolver
based on Apache Commons FileUpload is
not available anymore, as of Spring Framework 6.0 with its new Servlet 5.0+ baseline.
To enable multipart handling, you need to declare a MultipartResolver
bean in your
DispatcherServlet
Spring configuration with a name of multipartResolver
.
The DispatcherServlet
detects it and applies it to the incoming request. When a POST
with a content type of multipart/form-data
is received, the resolver parses the
content wraps the current HttpServletRequest
as a MultipartHttpServletRequest
to
provide access to resolved files in addition to exposing parts as request parameters.
Servlet Multipart Parsing
Servlet multipart parsing needs to be enabled through Servlet container configuration. To do so:
-
In Java, set a
MultipartConfigElement
on the Servlet registration. -
In
web.xml
, add a"<multipart-config>"
section to the servlet declaration.
The following example shows how to set a MultipartConfigElement
on the Servlet registration:
-
Java
-
Kotlin
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
// ...
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
// Optionally also set maxFileSize, maxRequestSize, fileSizeThreshold
registration.setMultipartConfig(new MultipartConfigElement("/tmp"));
}
}
class AppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() {
// ...
override fun customizeRegistration(registration: ServletRegistration.Dynamic) {
// Optionally also set maxFileSize, maxRequestSize, fileSizeThreshold
registration.setMultipartConfig(MultipartConfigElement("/tmp"))
}
}
Once the Servlet multipart configuration is in place, you can add a bean of type
StandardServletMultipartResolver
with a name of multipartResolver
.
This resolver variant uses your Servlet container’s multipart parser as-is,
potentially exposing the application to container implementation differences.
By default, it will try to parse any |