码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC文件上传——bean的配置【org.springframework.web.multipart.commons.CommonsMultipartResolver】

时间:2018-08-09 23:17:40      阅读:1346      评论:0      收藏:0      [点我收藏+]

标签:OWIN   line   directory   one   百度   value   编码格式   图片   nbsp   

一、简介

  Spring MVC支持一个通用的多路上传解析器CommonsMultipartResolver,在Spring的配置文件中对CommonsMultipartResolver Bean进行配置时,有一些可选的属性配置。

 


 

二、分析

  经过百度和查看SpringMVC的API文档都没有发现相关的详细配置介绍,无可奈何只能查看源代码寻找蛛丝马迹:

  在Spring配置文件applicationContext.xml中配置了CommonsMultipartResolver Bean后,按 “ Ctrl+鼠标左击 ‘org.springframework.web.multipart.commons.CommonsMultipartResolver’ ” 进入其源代码界面,而在源代码中并没有发现直接声明的如"maxUploadSize"这样的属性。

技术分享图片

   经过仔细查看,在源代码中,CommonsMultipartResolver类上的注释上看到了相关表述(如下标红加大字体):

 1 /**
 2  * Servlet-based {@link MultipartResolver} implementation for
 3  * <a href="http://commons.apache.org/proper/commons-fileupload">Apache Commons FileUpload</a>
 4  * 1.2 or above.
 5  *
 6  * <p>Provides "maxUploadSize", "maxInMemorySize" and "defaultEncoding" settings as
 7  * bean properties (inherited from {@link CommonsFileUploadSupport}). See corresponding
 8  * ServletFileUpload / DiskFileItemFactory properties ("sizeMax", "sizeThreshold",
 9  * "headerEncoding") for details in terms of defaults and accepted values.
10  *
11  * <p>Saves temporary files to the servlet container‘s temporary directory.
12  * Needs to be initialized <i>either</i> by an application context <i>or</i>
13  * via the constructor that takes a ServletContext (for standalone usage).
14  *
15  * @author Trevor D. Cook
16  * @author Juergen Hoeller
17  * @since 29.09.2003
18  * @see #CommonsMultipartResolver(ServletContext)
19  * @see #setResolveLazily
20  * @see org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver
21  * @see org.apache.commons.fileupload.servlet.ServletFileUpload
22  * @see org.apache.commons.fileupload.disk.DiskFileItemFactory
23  */
24 public class CommonsMultipartResolver extends CommonsFileUploadSupport
25         implements MultipartResolver, ServletContextAware {
26 ...

  可以看到,其中包含了"maxUploadSize", "maxInMemorySize" ,"defaultEncoding"三个可供在Bean中配置的属性:

  •  maxUploadSize :用于限制上传文件的最大尺寸,单位为字节;
  •  maxInMemorySize :读取文件到内存中最大的字节数(相当于缓存),默认是1024,单位为字节;
  •  defaultEncoding :表示请求的编码格式,默认为iso-8859-1。

  此外,在源代码中还可以发现包含了一个已声明的属性和相应的setter方法:

  属性:resolveLazily

1     private boolean resolveLazily = false;

  对应的setter方法:

 1     /**
 2      * Set whether to resolve the multipart request lazily at the time of
 3      * file or parameter access.
 4      * <p>Default is "false", resolving the multipart elements immediately, throwing
 5      * corresponding exceptions at the time of the {@link #resolveMultipart} call.
 6      * Switch this to "true" for lazy multipart parsing, throwing parse exceptions
 7      * once the application attempts to obtain multipart files or parameters.
 8      */
 9     public void setResolveLazily(boolean resolveLazily) {
10         this.resolveLazily = resolveLazily;
11     }

  因此,可以推断出,通过Spring的依赖注入功能,可以在Bean中配置和注入该属性,经过一番查询得知:

  •  resolveLazily :判断是否要延迟解析文件。当 resolveLazily为false(默认)时,会立即调用 parseRequest() 方法对请求数据进行解析,然后将解析结果封装到 DefaultMultipartHttpServletRequest中;而当resolveLazily为 true时,会在DefaultMultipartHttpServletRequest的initializeMultipart()方法调用parseRequest()方法对请求数据进行解析,而initializeMultipart()方法又是被getMultipartFiles()方法调用,即当需要获取文件信息时才会去解析请求数据,这种方式用了懒加载的思想。

 


 

 三、示例

  配置一个CommonsMultipartResolver Bean(XML):

     <!--配置文件上传使用解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 指定字符集为utf-8 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        
        <!-- 指定上传文件最大尺寸 -->
        <property name="maxUploadSize" value="10240"/>
        
        <!-- 指定文件载入内存大小 -->
        <property name="maxInMemorySize " value="1024"/>
        
        <!-- 设置延时解析文件 -->
        <property name="resolveLazily " value="true"/>
    </bean>

 


 

SpringMVC文件上传——bean的配置【org.springframework.web.multipart.commons.CommonsMultipartResolver】

标签:OWIN   line   directory   one   百度   value   编码格式   图片   nbsp   

原文地址:https://www.cnblogs.com/wangzhijun97/p/9451463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!