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

时间:2022-03-16 04:18:38

一、简介

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


二、分析

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

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

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

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

 /**
* Servlet-based {@link MultipartResolver} implementation for
* <a href="http://commons.apache.org/proper/commons-fileupload">Apache Commons FileUpload</a>
* 1.2 or above.
*
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.
*
* <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).
*
* @author Trevor D. Cook
* @author Juergen Hoeller
* @since 29.09.2003
* @see #CommonsMultipartResolver(ServletContext)
* @see #setResolveLazily
* @see org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver
* @see org.apache.commons.fileupload.servlet.ServletFileUpload
* @see org.apache.commons.fileupload.disk.DiskFileItemFactory
*/
public class CommonsMultipartResolver extends CommonsFileUploadSupport
implements MultipartResolver, ServletContextAware {
...

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

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

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

  属性:resolveLazily

     private boolean resolveLazily = false;

  对应的setter方法:

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

  因此,可以推断出,通过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>