Class CommonsMultipartResolver
- All Implemented Interfaces:
org.springframework.beans.factory.Aware,org.springframework.web.context.ServletContextAware,org.springframework.web.multipart.MultipartResolver
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.web.multipart.commons.CommonsFileUploadSupport
CommonsFileUploadSupport.MultipartParsingResult -
Field Summary
Fields inherited from class org.springframework.web.multipart.commons.CommonsFileUploadSupport
logger -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for use as bean.CommonsMultipartResolver(jakarta.servlet.ServletContext servletContext) Constructor for standalone usage. -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanupMultipart(org.springframework.web.multipart.MultipartHttpServletRequest request) protected StringdetermineEncoding(jakarta.servlet.http.HttpServletRequest request) Determine the encoding for the given request.booleanisMultipart(jakarta.servlet.http.HttpServletRequest request) protected org.apache.tomcat.util.http.fileupload.FileUploadnewFileUpload(org.apache.tomcat.util.http.fileupload.FileItemFactory fileItemFactory) Initialize the underlyingorg.apache.tomcat.util.http.fileupload.servlet.ServletFileUploadinstance.parseRequest(jakarta.servlet.http.HttpServletRequest request) Parse the given servlet request, resolving its multipart elements.org.springframework.web.multipart.MultipartHttpServletRequestresolveMultipart(jakarta.servlet.http.HttpServletRequest request) voidsetResolveLazily(boolean resolveLazily) Set whether to resolve the multipart request lazily at the time of file or parameter access.voidsetServletContext(jakarta.servlet.ServletContext servletContext) voidsetSupportedMethods(String... supportedMethods) Specify supported methods as an array of HTTP method names.Methods inherited from class org.springframework.web.multipart.commons.CommonsFileUploadSupport
cleanupFileItems, createMultipartFile, getDefaultEncoding, getFileItemFactory, getFileUpload, isUploadTempDirSpecified, newFileItemFactory, parseFileItems, prepareFileUpload, setDefaultEncoding, setMaxInMemorySize, setMaxUploadSize, setMaxUploadSizePerFile, setPreserveFilename, setUploadTempDir
-
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
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 underlyingorg.apache.tomcat.util.http.fileupload.servlet.ServletFileUploadinstance. Can be overridden to use a custom subclass, e.g. for testing purposes.- Specified by:
newFileUploadin classCommonsFileUploadSupport- Parameters:
fileItemFactory- the Commons FileItemFactory to use- Returns:
- the new ServletFileUpload instance
-
setServletContext
public void setServletContext(jakarta.servlet.ServletContext servletContext) - Specified by:
setServletContextin interfaceorg.springframework.web.context.ServletContextAware
-
isMultipart
public boolean isMultipart(jakarta.servlet.http.HttpServletRequest request) - Specified by:
isMultipartin interfaceorg.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:
resolveMultipartin interfaceorg.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
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:
cleanupMultipartin interfaceorg.springframework.web.multipart.MultipartResolver
-