Class CommonsMultipartResolver

java.lang.Object
org.springframework.web.multipart.commons.CommonsFileUploadSupport
org.springframework.web.multipart.commons.CommonsMultipartResolver
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.web.context.ServletContextAware, org.springframework.web.multipart.MultipartResolver

public class CommonsMultipartResolver extends CommonsFileUploadSupport implements org.springframework.web.multipart.MultipartResolver, org.springframework.web.context.ServletContextAware
Servlet-based MultipartResolver implementation for Apache Commons FileUpload 1.2 or above. This resolver variant delegates to a local FileUpload library within the application, providing maximum portability across Servlet containers.

Commons FileUpload traditionally parses POST requests with any "multipart/" type. Supported HTTP methods may be customized through setSupportedMethods(java.lang.String...).

Provides "maxUploadSize", "maxInMemorySize" and "defaultEncoding" settings as bean properties (inherited from CommonsFileUploadSupport). See corresponding ServletFileUpload / DiskFileItemFactory properties ("sizeMax", "sizeThreshold", "headerEncoding") for details in terms of defaults and accepted values.

Saves temporary files to the servlet container's temporary directory. Needs to be initialized either by an application context or via the constructor that takes a ServletContext (for standalone usage).

Note: The common alternative is StandardServletMultipartResolver, delegating to the Servlet container's own multipart parser, with configuration to happen at the container level and potentially with container-specific limitations.

Since:
29.09.2003
See Also:
  • Constructor Details

    • CommonsMultipartResolver

      public CommonsMultipartResolver()
      Constructor for use as bean. Determines the servlet container's temporary directory via the ServletContext passed in as through the ServletContextAware interface (typically by a WebApplicationContext).
      See Also:
    • CommonsMultipartResolver

      public CommonsMultipartResolver(jakarta.servlet.ServletContext servletContext)
      Constructor for standalone usage. Determines the servlet container's temporary directory via the given ServletContext.
      Parameters:
      servletContext - the ServletContext to use
  • Method Details

    • setResolveLazily

      public void setResolveLazily(boolean resolveLazily)
      Set whether to resolve the multipart request lazily at the time of file or parameter access.

      Default is "false", resolving the multipart elements immediately, throwing corresponding exceptions at the time of the resolveMultipart(jakarta.servlet.http.HttpServletRequest) call. Switch this to "true" for lazy multipart parsing, throwing parse exceptions once the application attempts to obtain multipart files or parameters.

    • setSupportedMethods

      public void setSupportedMethods(String... supportedMethods)
      Specify supported methods as an array of HTTP method names. The traditional Commons FileUpload default is "POST" only.

      When configured as a Spring property value, this can be a comma-separated String: e.g. "POST,PUT".

      Since:
      5.3.9
    • newFileUpload

      protected org.apache.tomcat.util.http.fileupload.FileUpload newFileUpload(org.apache.tomcat.util.http.fileupload.FileItemFactory fileItemFactory)
      Initialize the underlying org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload instance. Can be overridden to use a custom subclass, e.g. for testing purposes.
      Specified by:
      newFileUpload in class CommonsFileUploadSupport
      Parameters:
      fileItemFactory - the Commons FileItemFactory to use
      Returns:
      the new ServletFileUpload instance
    • setServletContext

      public void setServletContext(jakarta.servlet.ServletContext servletContext)
      Specified by:
      setServletContext in interface org.springframework.web.context.ServletContextAware
    • isMultipart

      public boolean isMultipart(jakarta.servlet.http.HttpServletRequest request)
      Specified by:
      isMultipart in interface org.springframework.web.multipart.MultipartResolver
    • resolveMultipart

      public org.springframework.web.multipart.MultipartHttpServletRequest resolveMultipart(jakarta.servlet.http.HttpServletRequest request) throws org.springframework.web.multipart.MultipartException
      Specified by:
      resolveMultipart in interface org.springframework.web.multipart.MultipartResolver
      Throws:
      org.springframework.web.multipart.MultipartException
    • parseRequest

      protected CommonsFileUploadSupport.MultipartParsingResult parseRequest(jakarta.servlet.http.HttpServletRequest request) throws org.springframework.web.multipart.MultipartException
      Parse the given servlet request, resolving its multipart elements.
      Parameters:
      request - the request to parse
      Returns:
      the parsing result
      Throws:
      org.springframework.web.multipart.MultipartException - if multipart resolution failed.
    • determineEncoding

      protected String determineEncoding(jakarta.servlet.http.HttpServletRequest request)
      Determine the encoding for the given request. Can be overridden in subclasses.

      The default implementation checks the request encoding, falling back to the default encoding specified for this resolver.

      Parameters:
      request - current HTTP request
      Returns:
      the encoding for the request (never null)
      See Also:
    • cleanupMultipart

      public void cleanupMultipart(org.springframework.web.multipart.MultipartHttpServletRequest request)
      Specified by:
      cleanupMultipart in interface org.springframework.web.multipart.MultipartResolver