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.spring-doc.cn

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.spring-doc.cn

Servlet Multipart Parsing

Servlet multipart parsing needs to be enabled through Servlet container configuration. To do so:spring-doc.cn

  • In Java, set a MultipartConfigElement on the Servlet registration.spring-doc.cn

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

The following example shows how to set a MultipartConfigElement on the Servlet registration:spring-doc.cn

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected String[] getServletMappings() {
		...
	}

	@Override
	protected Class<?> @Nullable [] getRootConfigClasses() {
		...
	}

	@Override
	protected Class<?> @Nullable [] getServletConfigClasses() {
		...
	}

	@Override
	protected void customizeRegistration(ServletRegistration.Dynamic registration) {

		// Optionally also set maxFileSize, maxRequestSize, fileSizeThreshold
		registration.setMultipartConfig(new MultipartConfigElement("/tmp"));
	}
}
class AppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() {

	override fun getServletMappings(): Array<out String> {
		...
	}

	override fun getRootConfigClasses(): Array<out Class<*>>? {
		...
	}

	override fun getServletConfigClasses(): Array<out Class<*>>? {
		...
	}

	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.spring-doc.cn

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 multipart/ content type with any HTTP method but this may not be supported across all Servlet containers. See the StandardServletMultipartResolver javadoc for details and configuration options.spring-doc.cn