接口 MultipartResolver
public interface MultipartResolver
A strategy interface for multipart file upload resolution in accordance with RFC 1867. Implementations are typically usable both within an application context and standalone.There are two concrete implementations included in Spring, as of Spring 3.1:
CommonsMultipartResolverfor Apache Commons FileUploadStandardServletMultipartResolverfor 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 aDispatcherServlet'sapplication context. Such a resolver gets applied to all requests handled by thatDispatcherServlet.If a
DispatcherServletdetects a multipart request, it will resolve it via the configuredMultipartResolverand pass on a wrappedHttpServletRequest. Controllers can then cast their given request to theMultipartHttpServletRequestinterface, which allows for access to anyMultipartFiles. 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 aByteArrayMultipartFileEditororStringMultipartFileEditorwith their data binder, to automatically apply multipart content to form bean properties.As an alternative to using a
MultipartResolverwith aDispatcherServlet, aMultipartFiltercan be registered inweb.xml. It will delegate to a correspondingMultipartResolverbean 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
MultipartResolveritself from application code. It will simply do its work behind the scenes, makingMultipartHttpServletRequestsavailable to controllers.- 从以下版本开始:
- 29.09.2003
- 作者:
- Juergen Hoeller, Trevor D. Cook
- 另请参阅:
MultipartHttpServletRequest,MultipartFile,CommonsMultipartResolver,ByteArrayMultipartFileEditor,StringMultipartFileEditor,DispatcherServlet
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 voidcleanupMultipart(MultipartHttpServletRequest request)Cleanup any resources used for the multipart handling, like a storage for the uploaded files.booleanisMultipart(HttpServletRequest request)Determine if the given request contains multipart content.MultipartHttpServletRequestresolveMultipart(HttpServletRequest request)Parse the given HTTP request into multipart files and parameters, and wrap the request inside aMultipartHttpServletRequestobject that provides access to file descriptors and makes contained parameters accessible via the standard ServletRequest methods.
方法详细资料
isMultipart
boolean isMultipart(HttpServletRequest request)
Determine if the given request contains multipart content.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 evaluated- 返回:
- whether the request contains multipart content
resolveMultipart
MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException
Parse the given HTTP request into multipart files and parameters, and wrap the request inside aMultipartHttpServletRequestobject 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)- 返回:
- the wrapped servlet request
- 抛出:
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()
cleanupMultipart
void cleanupMultipart(MultipartHttpServletRequest request)
Cleanup any resources used for the multipart handling, like a storage for the uploaded files.- 参数:
request- the request to cleanup resources for