CommonsMultipartResolverUploadController
package com.jege.spring.mvc;
import java.io.File;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller
public class CommonsMultipartResolverUploadController{
// http://localhost:8080/upload
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String uploadGet() {
System.out.println("先以get的请求方式显示上传页面");
return "upload";
}
int i = 0;// 简单改变上传文件的名字
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String uploadPost(HttpServletRequest request) throws Exception {
System.out.println("在以post请求方式处理上传的请求");
// 记录开始上传文件的时间
long startTime = System.currentTimeMillis();
String webapp = request.getServletContext().getRealPath("/upload");
System.out.println("上传文件保存到服务器webapp的路径:" + webapp);
// 存放文件的路径
File deskFile = new File(webapp, "eclipse.zip");
System.out.println("上传文件存放的最终路径:" + deskFile.getAbsolutePath());
// 将当前上下文初始化给 CommonsMutipartResolver:配置上传文件解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
// 检查form中是否有enctype="multipart/form-data",即是是否有文件上传
if (multipartResolver.isMultipart(request)) {
// 将request变成多部件MultipartHttpServletRequest
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 获取multiRequest 中所有的文件名
Iterator<String> iter = multiRequest.getFileNames();
// 遍历所有文件
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {// 处理上传
file.transferTo(deskFile);
}
}
}
// 添加提示信息
request.setAttribute("msg", "上传成功");
// 记录结束上传文件的时间
long endTime = System.currentTimeMillis();
System.out.println("上传究竟用了多少时间:" + String.valueOf(endTime - startTime) + "ms");
// 直接转发到upload.jsp
return "upload";
}
}
上传300MB文件耗时比较
- 使用CommonsMultipartResolver来保存上传的文件
先以get的请求方式显示上传页面
在以post请求方式处理上传的请求
上传究竟用了多少时间:261ms
第二次上传究竟用了多少时间:223ms
第三次上传究竟用了多少时间:276ms
其他关联项目
- SpringMVC-文件上传-配置
http://blog.csdn.net/je_ge/article/details/53725357 - SpringMVC-文件上传-MultipartFile
http://blog.csdn.net/je_ge/article/details/53726147
源码地址
https://github.com/je-ge/spring
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!