标签:
@RequestMapping(value = "/upload")
public String updateThumb(HttpServletRequest request,
HttpServletResponse response, ModelMap mmMap)
throws IllegalStateException, IOException {
// 创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(request)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
//对上传文件进行处理
if(!file.isEmpty()){
String path = request.getSession().getServletContext()
.getRealPath("upload");
String fileName = file.getOriginalFilename();
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//保存
try {
file.transferTo(targetFile);
System.out.println("保存文件【"+file.getOriginalFilename()+"】完成...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return "success";
}
【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
标签:
原文地址:http://www.cnblogs.com/ssslinppp/p/4607330.html