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中配置的属性:
此外,在源代码中还可以发现包含了一个已声明的属性和相应的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中配置和注入该属性,经过一番查询得知:
配置一个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】
原文:https://www.cnblogs.com/wangzhijun97/p/9451463.html