public class CommonsMultipartResolver extends CommonsFileUploadSupport implements MultipartResolver, ServletContextAware
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.
CommonsMultipartResolver(ServletContext)
,
setResolveLazily(boolean)
,
setSupportedMethods(java.lang.String...)
,
ServletFileUpload
,
DiskFileItemFactory
,
StandardServletMultipartResolver
CommonsFileUploadSupport.MultipartParsingResult
logger
Constructor and Description |
---|
CommonsMultipartResolver()
Constructor for use as bean.
|
CommonsMultipartResolver(ServletContext servletContext)
Constructor for standalone usage.
|
Modifier and Type | Method and Description |
---|---|
void |
cleanupMultipart(MultipartHttpServletRequest request)
Clean up any resources used for the multipart handling,
like a storage for the uploaded files.
|
protected String |
determineEncoding(HttpServletRequest request)
Determine the encoding for the given request.
|
boolean |
isMultipart(HttpServletRequest request)
Determine if the given request contains multipart content.
|
protected org.apache.commons.fileupload.FileUpload |
newFileUpload(org.apache.commons.fileupload.FileItemFactory fileItemFactory)
Initialize the underlying
org.apache.commons.fileupload.servlet.ServletFileUpload
instance. |
protected CommonsFileUploadSupport.MultipartParsingResult |
parseRequest(HttpServletRequest request)
Parse the given servlet request, resolving its multipart elements.
|
MultipartHttpServletRequest |
resolveMultipart(HttpServletRequest request)
Parse the given HTTP request into multipart files and parameters,
and wrap the request inside a
MultipartHttpServletRequest
object that provides access to file descriptors and makes contained
parameters accessible via the standard ServletRequest methods. |
void |
setResolveLazily(boolean resolveLazily)
Set whether to resolve the multipart request lazily at the time of
file or parameter access.
|
void |
setServletContext(ServletContext servletContext)
Set the
ServletContext that this object runs in. |
void |
setSupportedMethods(String... supportedMethods)
Specify supported methods as an array of HTTP method names.
|
cleanupFileItems, createMultipartFile, getDefaultEncoding, getFileItemFactory, getFileUpload, isUploadTempDirSpecified, newFileItemFactory, parseFileItems, prepareFileUpload, setDefaultEncoding, setMaxInMemorySize, setMaxUploadSize, setMaxUploadSizePerFile, setPreserveFilename, setUploadTempDir
public CommonsMultipartResolver()
public CommonsMultipartResolver(ServletContext servletContext)
servletContext
- the ServletContext to usepublic void setResolveLazily(boolean resolveLazily)
Default is "false", resolving the multipart elements immediately, throwing
corresponding exceptions at the time of the resolveMultipart(javax.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.
public void setSupportedMethods(String... supportedMethods)
When configured as a Spring property value, this can be a comma-separated String: e.g. "POST,PUT".
protected org.apache.commons.fileupload.FileUpload newFileUpload(org.apache.commons.fileupload.FileItemFactory fileItemFactory)
org.apache.commons.fileupload.servlet.ServletFileUpload
instance. Can be overridden to use a custom subclass, e.g. for testing purposes.newFileUpload
in class CommonsFileUploadSupport
fileItemFactory
- the Commons FileItemFactory to usepublic void setServletContext(ServletContext servletContext)
ServletContextAware
ServletContext
that this object runs in.
Invoked after population of normal bean properties but before an init
callback like InitializingBean's afterPropertiesSet
or a
custom init-method. Invoked after ApplicationContextAware's
setApplicationContext
.
setServletContext
in interface ServletContextAware
servletContext
- the ServletContext object to be used by this objectInitializingBean.afterPropertiesSet()
,
ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
public boolean isMultipart(HttpServletRequest request)
MultipartResolver
Will typically check for content type "multipart/form-data", but the actually accepted requests might depend on the capabilities of the resolver implementation.
isMultipart
in interface MultipartResolver
request
- the servlet request to be evaluatedpublic MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException
MultipartResolver
MultipartHttpServletRequest
object that provides access to file descriptors and makes contained
parameters accessible via the standard ServletRequest methods.resolveMultipart
in interface MultipartResolver
request
- the servlet request to wrap (must be of a multipart content type)MultipartException
- if the servlet request is not multipart, or if
implementation-specific problems are encountered (such as exceeding file size limits)MultipartRequest.getFile(java.lang.String)
,
MultipartRequest.getFileNames()
,
MultipartRequest.getFileMap()
,
ServletRequest.getParameter(java.lang.String)
,
ServletRequest.getParameterNames()
,
ServletRequest.getParameterMap()
protected CommonsFileUploadSupport.MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException
request
- the request to parseMultipartException
- if multipart resolution failed.protected String determineEncoding(HttpServletRequest request)
The default implementation checks the request encoding, falling back to the default encoding specified for this resolver.
request
- current HTTP requestnull
)ServletRequest.getCharacterEncoding()
,
CommonsFileUploadSupport.setDefaultEncoding(java.lang.String)
public void cleanupMultipart(MultipartHttpServletRequest request)
MultipartResolver
cleanupMultipart
in interface MultipartResolver
request
- the request to clean up resources for