首页 > 代码库 > 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;
}

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




Spring的文件上传