本文通过实例代码给大家讲解了SpringMvc+Angularjs 实现多文件批量上传功能,非常不错,具有参考借鉴价值,需要的朋友一起学习吧
SpringMvc代码
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表单提交