MultipartResolver from the org.springframework.web.multipart package is a strategy for parsing multi-part requests, including file uploads. There is one implementation based on FileUpload from the Commons project, and another based on multipart query parsing Servlet 3.0.

To enable multipart processing, you must declare a MultipartResolver bean in the DispatcherServlet configuration from Spring under the name multipartResolver. DispatcherServlet will detect it and apply it to the incoming request. Upon receiving a POST with content type multipart/form-data, the resolver parses the content by wrapping the current HttpServletRequest as MultipartHttpServletRequest to provide access to recognized files in addition to exposing components as query parameters.

FileUpload from Apache Commons

To use FileUpload from Apache Commons, you can configure a bean of type CommonsMultipartResolver called multipartResolver. You also need to have the commons-fileupload jar file as a dependency in your classpath.

This resolver variant delegates authority to a local library within the application, providing maximum cross-platform portability between servlet containers. Alternatively, you can use standard multi-part servlet recognition through the container's own parser, as described below.

Commons FileUpload traditionally only applies to POST requests, but accepts any multipart/ content type. For details and configuration options, see the javadoc at CommonsMultipartResolver.

Servlet 3.0

Multipart parsing according to the Servlet 3.0 specification must be enabled in the servlet container configuration. To do this:

  • In Java, set MultipartConfigElement to register servlets.

  • In web.xml, add a "<multipart-config>" section to the servlet declaration.

The following example shows how to configure MultipartConfigElement to register servlets:

Java
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    // ...
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        // Optionally also sets maxFileSize, maxRequestSize, fileSizeThreshold
        registration.setMultipartConfig(new MultipartConfigElement("/tmp"));
    }
}
Kotlin
class AppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() {
    // ...
    override fun customizeRegistration(registration: ServletRegistration.Dynamic) {
        // Optionally also sets maxFileSize, maxRequestSize, fileSizeThreshold
        registration.setMultipartConfig(MultipartConfigElement("/tmp"))
    }
}

Once the Servlet 3.0 configuration is created, you can add a bean of type StandardServletMultipartResolver called multipartResolver.

This recognition option uses your servlet container's multi-part parser as is, potentially exposing the application to differences in the container's implementation. By default, the parser will attempt to parse any multipart/ content type using any HTTP method, but this may not be supported by all servlet containers. For details and configuration options, see the javadoc at StandardServletMultipartResolver.