public interface MultipartResolver
There are two concrete implementations included in Spring, as of Spring 3.1:
CommonsMultipartResolver
for Apache Commons FileUpload
StandardServletMultipartResolver
for the Servlet 3.0+ Part API
There is no default resolver implementation used for Spring
DispatcherServlets
,
as an application might choose to parse its multipart requests itself. To define
an implementation, create a bean with the id "multipartResolver" in a
DispatcherServlet's
application context. Such a resolver gets applied to all requests handled
by that DispatcherServlet
.
If a DispatcherServlet
detects a
multipart request, it will resolve it via the configured MultipartResolver
and pass on a wrapped HttpServletRequest
. Controllers
can then cast their given request to the MultipartHttpServletRequest
interface, which allows for access to any MultipartFiles
.
Note that this cast is only supported in case of an actual multipart request.
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile("image"); ... }Instead of direct access, command or form controllers can register a
ByteArrayMultipartFileEditor
or StringMultipartFileEditor
with their data binder, to automatically apply multipart content to form
bean properties.
As an alternative to using a MultipartResolver
with a
DispatcherServlet
,
a MultipartFilter
can be
registered in web.xml
. It will delegate to a corresponding
MultipartResolver
bean in the root application context. This is mainly
intended for applications that do not use Spring's own web MVC framework.
Note: There is hardly ever a need to access the MultipartResolver
itself from application code. It will simply do its work behind the scenes,
making MultipartHttpServletRequests
available to controllers.
MultipartHttpServletRequest
,
MultipartFile
,
CommonsMultipartResolver
,
ByteArrayMultipartFileEditor
,
StringMultipartFileEditor
,
DispatcherServlet
Modifier and Type | Method and Description |
---|---|
void |
cleanupMultipart(MultipartHttpServletRequest request)
Cleanup any resources used for the multipart handling,
like a storage for the uploaded files.
|
boolean |
isMultipart(HttpServletRequest request)
Determine if the given request contains multipart content.
|
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. |
boolean isMultipart(HttpServletRequest request)
Will typically check for content type "multipart/form-data", but the actually accepted requests might depend on the capabilities of the resolver implementation.
request
- the servlet request to be evaluatedMultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException
MultipartHttpServletRequest
object that provides access to file descriptors and makes contained
parameters accessible via the standard ServletRequest methods.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()
void cleanupMultipart(MultipartHttpServletRequest request)
request
- the request to cleanup resources for