首页 > 代码库 > Spring的文件上传

Spring的文件上传

Spring在发现包含multipart的请求后,会使用MultipartResolver的实现bean处理文件上传操作,现有采用Servlet3的
org.springframework.web.multipart.support.StandardServletMultipartResolver

 和采用commons-fileupload的

org.springframework.web.multipart.commons.CommonsMultipartResolver

处理文件的上传需要重写接口MultipartResolver的parseRequest方法。

其涉及到的成员变量类型:

public CommonsFileUploadSupport() {this.fileItemFactory = newFileItemFactory();
<span style="white-space:pre">	</span>this.fileUpload = newFileUpload(getFileItemFactory());
}
protected DiskFileItemFactory newFileItemFactory() {
	return new DiskFileItemFactory();
}


DiskFileItemFactory#createItem

public FileItem createItem(String fieldName, String contentType,
		boolean isFormField, String fileName) {
	DiskFileItem result = new DiskFileItem(fieldName, contentType,
			isFormField, fileName, sizeThreshold, repository);
	FileCleaningTracker tracker = getFileCleaningTracker();
	if (tracker != null) {
		tracker.track(result.getTempFile(), this);
	}
	return result;
}
DiskFileItem#getOutputStream

public OutputStream getOutputStream()
	throws IOException {
	if (dfos == null) {
		File outputFile = getTempFile();
		dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
	}
	return dfos;
}

其默认采用临时文件保存上传内容,若超过指定的内存大小限制,则直接存储为临时文件