页面代码:
<form name="form1" action="uploadFileJunior.html" method="post" ENCTYPE="multipart/form-data"> <input id="File1" name="fileupload" accept="file/*.*" multiple="multiple" type="file" value="" /> <input id="" type="submit" value="上传" /> </form>
上面实现多选的关键是multiple="multiple"一句。
后台处理代码(后台是SpringMVC的Controller):
@RequestMapping("/uploadFileJunior") public String uploadFileJunior(@RequestParam("fileupload") MultipartFile[] files,HttpServletRequest request,HttpServletResponse response){ try { int count=files.length; for(int i=0;i<count;i++){ MultipartFile file=files[i]; InputStream is=file.getInputStream(); String fileName=file.getOriginalFilename(); System.out.println("File"+(i+1)+":"+fileName); String newFilepath=fileService.upload2Server(is,fileName); logger.info(i+":"+file.getOriginalFilename()+" "+" newFilepath="+newFilepath); } return "/pages/sample/index.jsp"; } catch (Exception e) { e.printStackTrace(); logger.error(e); request.setAttribute("error", e.getClass()); request.setAttribute("reason", e.getMessage()); StackTraceElement[] arr=e.getStackTrace(); request.setAttribute("stackTraceElements", arr); return "pages/error/index.jsp"; } }
注意好上面两个fileupload的映射关系,后台这边对应的变量数组是files,之后针对files进行处理就行了。
参考资料: