SpringMVC(十五):在web.xml配置文件中ContextLoadListener与DispatcherServlet起到的作用

从Spring官网上可以看到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:

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.

关于MultipartResolver的接口文档,请参考Spring5.2.x的官方文档《https://docs.spring.io/spring/docs/5.2.x/javadoc-api/org/springframework/web/multipart/MultipartResolver.html

MultipartResolver接口定义如下:

public interface 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是一个为多文件上传提供了解决方案的策略接口,将普通的request封装成MultipartHttpServletRequest,在Spring中常见的两个实现方式,分别是:

1)StandardServletMultipartResolver:使用Servlet3.0标准上传方式,将HttpServletRequest转化为StandardServletMultipartResolver,以tomcat为例,从 Tomcat 7.0.x的版本开始就支持 Servlet 3.0了。

2)CommonsMultipartResolver:使用apache的common-fileupload,将HttpServletRequest转化为DefaultMultipartHttpServletRequest(需要依赖common-fileupload.jar,common-io.jar)。

在SpringMVC中MultipartResolver组件没有提供默认值,实际上如果上传文件不使用MultipartResovler组件封装成MultipartHttpServletRequest,直接用原生HttpServletRequest request也是可以的。

猜你喜欢

转载自www.cnblogs.com/yy3b2007com/p/11783661.html
今日推荐