码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC批量上传

时间:2015-11-17 18:35:45      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

@RequestMapping(value = "/upload")
 public String handleFormUpload(MultipartHttpServletRequest request){  
  String b = request.getParameter("a");
  System.out.println(b+"aaaaaaaaaaaaaaaaaaa");
  List<MultipartFile> file = request.getFiles("file");
  String path = this.servletContext.getRealPath("/tmp/"); // 获取本地存储路径
  System.out.println(path + "-----------------");
  System.out.println(file.size()+"=====================");
  FileOutputStream fileOutputStream = null;
  for (int i = 0; i < file.size(); i++) {
   if (!file.get(i).isEmpty()) {
    String fileName =  file.get(i).getOriginalFilename();
    String a = fileName.split("\\.")[1];
    File files = new File(path + new Date().getTime() +i+ "."+a); // 新建一个文件
    try {
     fileOutputStream = new FileOutputStream(files);
     fileOutputStream.write(file.get(i).getBytes());
     System.out.println(file.get(i).getOriginalFilename()+ "-----------------");
     System.out.println(file.get(i).getContentType()+ "-----------------");
     fileOutputStream.flush();
    } catch (Exception e) {
     e.printStackTrace();
    }
    if (fileOutputStream != null) { // 关闭流
     try {
      fileOutputStream.close();
     } catch (IOException ie) {
      ie.printStackTrace();
     }
    }
   }
  }
   return "redirect:top";
 }
<form method="post"&nbsp; enctype="multipart/form-data" action="<c:url value="fileUpLoad/upload.html"/>" >
&nbsp; <input&nbsp; type="text" name="a"/>
&nbsp;&nbsp; <input type="file" name="file" />
&nbsp;&nbsp; <input type="file" name="file" />
&nbsp;&nbsp; <input type="file" name="file" />
&nbsp;&nbsp; <input type="file" name="file" />
&nbsp;&nbsp; <input type="file" name="file" />
&nbsp;&nbsp; <input type="submit" />
&nbsp; </form> 

 

SpringMVC批量上传

标签:

原文地址:http://www.cnblogs.com/zhujiabin/p/4972372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!