Multipart Resolver

org.springframework.web.multipart 包中的 MultipartResolver 是一种策略,用于解析包括文件上传在内的多部分请求。有一个基于容器的 StandardServletMultipartResolver 实现,用于 Servlet 多部分请求解析。请注意,已经过时的 CommonsMultipartResolver(基于 Apache Commons FileUpload)不再可用了,自 Spring Framework 6.0 及其新的 Servlet 5.0+ 基线以来。

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.

要启用多部分处理,你需要在 DispatcherServlet Spring 配置中声明一个 MultipartResolver Bean,其名称为 multipartResolverDispatcherServlet 会检测它并将其应用于传入的请求。当接收到内容类型为 multipart/form-data 的 POST 请求时,解析器解析内容,将当前的 HttpServletRequest 包装为 MultipartHttpServletRequest,除了将部分公开为请求参数,还提供对已解析文件的访问。

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 容器配置来启用 Servlet 多部分解析。具体做法为:

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.

下面的示例展示了如何在 Servlet 注册中设置一个 MultipartConfigElement

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"))
	}

}

一旦 Servlet 多部分配置就绪,你就可以添加一个名为 multipartResolver 且类型为 StandardServletMultipartResolver 的 Bean。

Once the Servlet multipart configuration is in place, you can add a bean of type StandardServletMultipartResolver with a name of multipartResolver.

该解析器变体会按原样使用您的 Servlet 容器的多部件解析器,可能导致应用程序受到容器实现差异的影响。默认情况下,它将尝试使用任何 HTTP 方法解析任何 multipart/ 内容类型,但这在所有 Servlet 容器中可能不受支持。有关详细信息和配置选项,请参阅https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/support/StandardServletMultipartResolver.html[StandardServletMultipartResolver] javadoc。

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.