1、MultipartResolver
MultipartResolver接口提供了spring mvc的上传视图,MultipartResolver实例在请求转交给handlermapping之前。 也就是说DispatcherServlet ,在请求映射的时候,若找到MultipartResolver这个实例的话,
就直接把请求转交给MultipartResolver
/** * Determine if the given request contains multipart content. * <p>Will typically check for content type "multipart/form-data", but the actually * accepted requests might depend on the capabilities of the resolver implementation. * @param request the servlet request to be evaluated * @return whether the request contains multipart content */ boolean isMultipart(HttpServletRequest request); /** * Parse the given HTTP request into multipart files and parameters, * and wrap the request inside a * {@link org.springframework.web.multipart.MultipartHttpServletRequest} * object that provides access to file descriptors and makes contained * parameters accessible via the standard ServletRequest methods. * @param request the servlet request to wrap (must be of a multipart content type) * @return the wrapped servlet request * @throws MultipartException if the servlet request is not multipart, or if * implementation-specific problems are encountered (such as exceeding file size limits) * @see MultipartHttpServletRequest#getFile * @see MultipartHttpServletRequest#getFileNames * @see MultipartHttpServletRequest#getFileMap * @see javax.servlet.http.HttpServletRequest#getParameter * @see javax.servlet.http.HttpServletRequest#getParameterNames * @see javax.servlet.http.HttpServletRequest#getParameterMap */ MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException; /** * Cleanup any resources used for the multipart handling, * like a storage for the uploaded files. * @param request the request to cleanup resources for */ void cleanupMultipart(MultipartHttpServletRequest request);
MultipartResolver接口提供了3个方法:
boolean isMultipart(HttpServletRequest request): 检查当前请求是否是multipart content type
resolveMultipart: 将httprequest转换成MultipartHttpServletRequest
已知的子类: CommonsMultipartResolver, StandardServletMultipartResolver
- 第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,但它能在比较旧的 servlet 版本中使用。
- 第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,但是只能在 Servlet 3 以上的版本使用。
2、multipartRequest
multipartRequest接口可以讲request对象封装成multipartRequest,提取请求的信息,MultipartReques类主要是对文件上传进行的处理。