SpringMVC批量上传

@RequestMapping(value = "/upload")
 public String handleFormUpload(MultipartHttpServletRequest request){  
  String b = request.getParameter("a");
  System.out.println(b+"aaaaaaaaaaaaaaaaaaa");
  List 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";
 }
" >                      

 

你可能感兴趣的:(Spring)