1. 配置Spring 文件类型解析器
2. 配置单文件上传
2.1.前端上传代码
<%--action前面的"/" 加上表示使用绝对路径,反之为相对路径--%>
2.2 开发Action
@RequestMapping("getFile.do") public ModelAndView getFile(HttpServletRequest request, @RequestParam("imgCard") MultipartFile multipartFile) throws IOException { String name = request.getParameter("name"); System.out.println("name = " + name); //得到文件的名称; String originalFilename = multipartFile.getOriginalFilename(); //希望存储的目标地址 File file = new File("C:\\Users\\lyuweigh\\Desktop\\"+originalFilename); multipartFile.transferTo(file); ModelAndView mv = new ModelAndView(); mv.setViewName("index.jsp"); mv.addObject("demo", "ojbk"); return mv; }
- MultipartFile ,用来存储对应上传的文件
- @RequestParam("xx") ,接受指定的name名称值
3. 配置多文件上传
3.1.前端上传代码
3.2 开发Action
@RequestMapping("getFiles.do") public ModelAndView getFiles(HttpServletRequest request, @RequestParam("imgCards") CommonsMultipartFile[] files) throws IOException { String name = request.getParameter("name"); System.out.println("name = " + name); for (CommonsMultipartFile file : files) { String originalFilename = file.getOriginalFilename(); file.transferTo(new File("C:\\Users\\lyuweigh\\Desktop\\"+originalFilename)); } ModelAndView mv = new ModelAndView(); mv.setViewName("index.jsp"); mv.addObject("demo", "ojbks"); return mv; }