SpringMVC文件上传——bean的配置【org.springframework.web.multipart.commons.CommonsMultipartResolver】
一、简介
Spring MVC支持一个通用的多路上传解析器CommonsMultipartResolver,在Spring的配置文件中对CommonsMultipartResolver Bean进行配置时,有一些可选的属性配置。
二、分析
经过百度和查看SpringMVC的API文档都没有发现相关的详细配置介绍,无可奈何只能查看源代码寻找蛛丝马迹:
在Spring配置文件applicationContext.xml中配置了CommonsMultipartResolver Bean后,按 “ Ctrl+鼠标左击 ‘org.springframework.web.multipart.commons.CommonsMultipartResolver’ ” 进入其源代码界面,而在源代码中并没有发现直接声明的如"maxUploadSize"这样的属性。
经过仔细查看,在源代码中,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>
SpringMVC文件上传——bean的配置【org.springframework.web.multipart.commons.CommonsMultipartResolver】的更多相关文章
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile(转)
文件上传项目的源码下载地址:http://download.csdn.net/detail/swingpyzf/6979915 一.配置文件:SpringMVC 用的是 的MultipartFil ...
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置MultipartResolver:用于处理表单中的file <!-- 配置Multipa ...
- SpringMVC文件上传的配置
记述一下步骤以备查. 准备工作: 需要把Jakarta Commons FileUpload及Jakarta Commons io的包放lib里. 我这边的包是: commons-fileupload ...
- springmvc 文件上传实现(不是服务器的)
1.spring使用了apache-commons下的上传组件,因此,我们需要引用2个jar包 1)apache-commons-fileupload.jar 2 ) apache-commons-i ...
- SpringMVC文件上传和下载
上传与下载 1文件上传 1.1加入jar包 文件上传需要依赖的jar包 1.2配置部件解析器 解析二进制流数据. <?xml version="1.0" encoding=& ...
- springMVC文件上传
参考的地址:http://www.tuicool.com/articles/nMVjaiF 1.需要使用的jar. commons-fileupload.jar与commons-io-1.4.jar二 ...
- SpringMVC 文件上传&拦截器&异常处理
文件上传 Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的.Spring 用 Jakarta Commons FileUpload ...
- SpringMVC文件上传下载
在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...
- SpringMVC文件上传实现
SpringMVC(注解)上传文件需要注意的几个地方:1.form的enctype="multipart/form-data",这个是上传文件必须的2.applicationCon ...
随机推荐
- canvas的globalAlphaAPI
canvas的globalAlphaAPI
- Native Method
While a 100% pure Java solution is nice in principle, realistically, for an application, there are s ...
- Azure本月最新活动,速度Mark!!
缤纷五月,翠色盈盈,风光如画,小编在这里给大家汇总了这个多彩五月最新的活动合集.我们一切都准备好了,就等你来参加了~ 首先最重磅的当然是新一届的全球微软开发者大会! 有吃有喝有 Build,5 月 ...
- 监控系统-nagios
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/ install yum -y install nagios-4 ...
- 关于VisualStudio2010发布项目问题
VisualStudio2010速度还是很给力的,VS2015打开机器就双100%了:VS2010机器上跑起来还是很好用的. 今天编译一个MVC3.0项目,发布时候出现诡异现象:Content文件夹里 ...
- Ajax使用 初始化数据 + mvc
2017-3-16 mvc+jquery+easyUI做项目 <input type="text" id="txtSTQty" name="tx ...
- mysql : 修改数据库权限
解决步骤 第一步,点击用户 注意!!! 编辑权限,在我们设置权限之前,我们需要先重新加载才能生效, 如果不用编辑的话,直接按重新载入编辑,这个相当于保存. 中文意思(注意看那段话) 第二步 选择要处理 ...
- org.apache.xmlbeans.XmlException: error: does not close tag
使用myeclipse的jax自动生成webservice , 或者serviceImpl通过@webservice来实现webservice时, 使用soap UI (我测试使用的版本 5.2.1) ...
- 2017.9.18 HTMl学习总结----input标签的额type
2.1.3 HTML表单标签与表单设计 (1)表单的组成:文本框(text),密码框(password),多行文本框(Multiline text box). 单选按钮框(Single - rad ...
- which,whereis,locate,find
which 查看可执行文件的位置 [root@redhat ~]# which passwd /usr/bin/passwd which是通过 PATH 环境变量到该路径内查找可执行文件,所 ...