control层实现功能:
@RequestMapping(value="upload2")public String upLoad2(HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException{ //解析器解析request的上下文 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //先判断request中是否包涵multipart类型的数据, if(multipartResolver.isMultipart(request)){ //再将request中的数据转化成multipart类型的数据 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator iter = multiRequest.getFileNames(); while(iter.hasNext()){ MultipartFile file = multiRequest.getFile((String)iter.next()); if(file != null){ String fileName = file.getOriginalFilename(); String path = "D:/" + fileName; File localFile = new File(path); //写文件到本地 file.transferTo(localFile); } } } return "/success"; }
JSP文件:
在springMVC配置文件中添加解析器: