SpringMVC按Ctrl上传多个文件
凌冰_ 人气:0JSP页面
注意:必须加入multiple="multiple"属性
<form action="upload2.action" method="post" enctype="multipart/form-data"> 照片:<input type="file" name="imageFile" multiple="multiple"/> <p /> <input type="submit" value="上传" /> </form>
Action类
@RequestMapping("/upload2") public String upload2(Model model, @RequestParam("imageFile") MultipartFile[] imageFile, HttpServletRequest request) throws Exception { //获得服务器路径 String path = request.getSession().getServletContext() .getRealPath("/images"); //创建服务器目录 File file = new File(path); if (!file.exists()) { file.mkdirs(); } // 集合存放路径 List<String> list = new ArrayList<String>(); // 遍历图片 for (MultipartFile multipartFile : imageFile) { // get fileName String fileName = multipartFile.getOriginalFilename(); System.out.println("上传路径=" + path + "/" + fileName); // get file path File saveFile = new File(path, fileName); // 将文件存储到目标中 multipartFile.transferTo(saveFile); // 保存到集合中 list.add("images/" + fileName); } // model.addAttribute("lists", list); return "display2.jsp"; }
显示页面
<c:forEach var="pic" items="${lists }"> <img alt="" src="${pic}"> </c:forEach>
效果如图所示: (用Google浏览器运行)
单击"打开"
单击"上传"
加载全部内容