spring mvc MultipartFile 上传文件 当文件较小时(10k) ,无法上传成功 。

<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="1024000"/>
<property name="fileItemFactory.sizeThreshold" value="0"></property>
</bean>

查看该类源码

有一段说明文档

* <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.

进入 CommonsFileUploadSupport

public abstract class CommonsFileUploadSupport {

	protected final Log logger = LogFactory.getLog(getClass());

	private final DiskFileItemFactory fileItemFactory;

	private final FileUpload fileUpload;

	private boolean uploadTempDirSpecified = false;

  进入

DiskFileItemFactory  类 
看到
/**
* The threshold above which uploads will be stored on disk.
*/
private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;//DEFAULT_SIZE_THRESHOLD=10240就是10k

设置下这个参数即可。

上一篇:Python爬虫之小试牛刀——使用Python抓取百度街景图像


下一篇:通过业务系统的重构实践DDD