SpringMvc+Angularjs 多文件批量上传

SpringMvc代码

jar包

  • commons-fileupload

  • commons-io

spring-mvc.xml配置


    

Controller

@RequestMapping(value = "api/v1/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map upload (@RequestParam(value = "files") MultipartFile [] files,
                                 @RequestParam(value = "id") String id,
                                 HttpServletRequest request, HttpServletResponse response) {
    Map res = new HashMap();
    try {
        log.info("upload>>>>>id:{}", id);
        if (files!=null) {
            for (MultipartFile file:files) {
                log.info("filename:{}", file.getOriginalFilename());
            }
        }
    } catch (Exception e) {
        log.error("upload>>>>异常:{}", e.toString());
    }
    log.info("upload>>>>返回结果:{}", res);
    return res;
}

保存到本地

 // copy File
 public boolean copyFile (MultipartFile tempFile, String filePath) {
     Boolean res = false;

     try {
         File file = new File(filePath);
         if (!file.getParentFile().exists()) {
             file.getParentFile().mkdirs();
         }
         // 将文件拷贝到当前目录下
         tempFile.transferTo(file);
         res = true;
     } catch (Exception e) {
         log.info("copyFile>>>>异常:{}", e.toString());
     }
     return res;
 }

AngularJs代码




    
    



Form表单提交

你可能感兴趣的:(SpringMvc+Angularjs 多文件批量上传)