Spring MVC之MultipartResolver

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30038111/article/details/83048905

MultipartResolverspring提供的文件上传解析器的接口,该接口有两个实现类:StandardServletMultipartResolverCommonsMultipartResolverMultipartResolver#isMultipart是判断是否文件上传的请求,post请求、contentType是否是以multipart/开头这两个是先决条件。

spring上下文中,有且只有一个该实例。 spring容器启动时初始化文件上传解析器:

DispatcherServlet#initStrategies(ApplicationContext context) {
	initMultipartResolver(context);
}
  • StandardServletMultipartResolver
    基于Servlet3.0 API实现,不需要引入额外的jar包,在spring的配置文件中注入此Bean时,不需要配置上传文件大小等参数,但是在web.xml中配置DispatcherServlet时,需要添加multipart-config配置。

    <servlet>
       <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:Spring_mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <multipart-config>
            <!--临时文件的目录-->
            <location>d:/</location>
            <!-- 上传文件的大小限制 -->
            <max-file-size>5242880</max-file-size>
            <!-- 一次表单提交中文件的大小限制 -->
            <max-request-size>10485760</max-request-size>
            <!-- 多大的文件会被自动保存到硬盘上  0:所有 -->
            <file-size-threshold>0</file-size-threshold>
        </multipart-config>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
     Standard implementation of the {@link MultipartResolver} interface,
     based on the Servlet 3.0 {@link javax.servlet.http.Part} API.
     To be added as "multipartResolver" bean to a Spring DispatcherServlet context,
     without any extra configuration at the bean level (see below).
    
     <p><b>Note:</b> In order to use Servlet 3.0 based multipart parsing,
     you need to mark the affected servlet with a "multipart-config" section in
     {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement}
     in programmatic servlet registration, or (in case of a custom servlet class)
     possibly with a {@link javax.servlet.annotation.MultipartConfig} annotation
     on your servlet class. Configuration settings such as maximum sizes or
     storage locations need to be applied at that servlet registration level;
     Servlet 3.0 does not allow for them to be set at the MultipartResolver level.
    
  • CommonsMultipartResolver
    基于Apache Commons FileUpload实现,继承自apacheCommonsFileUploadSupport。在spring中注入CommonsMultipartResolver时,可以配置"maxUploadSize", "maxInMemorySize" and "defaultEncoding"等属性。需要依赖jarcommons-fileupload、commons-io

    <bean id="multipartResolver"
    		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    		<!-- 其他配置参见CommonsFileUploadSupport中以set开头的方法 -->
    		<property name="defaultEncoding" value="UTF-8" />
    		<property name="maxUploadSize" value="5242880" />
    		<property name="maxInMemorySize" value="40960" />
    	</bean>
    
     Servlet-based {@link MultipartResolver} implementation for
     <a href="http://commons.apache.org/proper/commons-fileupload">Apache Commons FileUpload</a>
     1.2 or above.
     
     <p>Provides "maxUploadSize", "maxInMemorySize" and "defaultEncoding" settings as
     bean properties (inherited from {@link CommonsFileUploadSupport}). See corresponding
     ServletFileUpload / DiskFileItemFactory properties ("sizeMax", "sizeThreshold",
     "headerEncoding") for details in terms of defaults and accepted values.
     <p>Saves temporary files to the servlet container's temporary directory.
     Needs to be initialized <i>either</i> by an application context <i>or</i>
     via the constructor that takes a ServletContext (for standalone usage).
    

在controller中用MultipartFile和MultipartHttpServletRequest接收参数即可,具体可以尝试解析这两种方式有什么API

猜你喜欢

转载自blog.csdn.net/qq_30038111/article/details/83048905