首页 > 代码库 > SpringMVC文件上传

SpringMVC文件上传

 1 1.在SpringMVC中的配置文件dispatcherServlet-servlet.xml中加入 2  3     <!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver -->   4     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 5             <property name="maxUploadSize" value="http://www.mamicode.com/5000000"/> 6             <property name="defaultEncoding" value="http://www.mamicode.com/UTF-8"/> 7     </bean> 8  9 10 2.处理文件上传-单文件传-在controller类里添加11     //文件上传12     //添加用户时,为post请求,访问以下代码13     @RequestMapping(value="http://www.mamicode.com/adduser",method=RequestMethod.POST)14     public String add(@Validated User user,BindingResult bindingResult,MultipartFile attach,HttpServletRequest req){//紧跟validate之后写验证结果15         if(bindingResult.hasErrors()){16             System.out.println("@Validated User user-->"+user);17             //若有错误,直接掉转到add 视图18             return "user/add";19         }20         //判断文件是否为空21         if(!attach.isEmpty()){22             //指定文件上传位置23             String realPath =  req.getSession().getServletContext().getRealPath("/resources/upload");24             System.out.println("realPath===="+realPath);25             System.out.println("原始文件的名字"+attach.getOriginalFilename());26             System.out.println(attach.getContentType());27             System.out.println(attach.getName());//得到页面表单控件的值28             29             //建立空文件30             //File.separator="/",File.separator:路径自适应分隔符,适合其它操作系统, 31             File file = new File(realPath+File.separator+attach.getOriginalFilename());32             33             try {34                 FileUtils.copyInputStreamToFile(attach.getInputStream(), file);35                 //FileUtils.writeByteArrayToFile(file, attach.getBytes());36                 //attach.transferTo(file);37             } catch (Exception e) {38                 // TODO Auto-generated catch block39                 e.printStackTrace();40             }41             System.out.println(attach.getName()+" ======= "+attach.getOriginalFilename()+" ====== "+attach.getContentType());42         }43         userList.put(user.getUserName(), user);44         return "redirect:/user/userlist";45     }46     47     48 49 50 51 52 3.处理文件上传-多文件上传53 54     //添加用户时,为post请求,访问以下代码55     @RequestMapping(value="http://www.mamicode.com/adduser",method=RequestMethod.POST)56     public String add(@Validated User user,BindingResult bindingResult,@RequestParam("attachs") MultipartFile[] attachs,HttpServletRequest req){//紧跟validate之后写验证结果57         if(bindingResult.hasErrors()){58             //若有错误,直接掉转到add 视图59             return "user/add";60         }61         62         String realPath =  req.getSession().getServletContext().getRealPath("/resources/upload");63         System.out.println(realPath);64         //循环65         for(MultipartFile attach:attachs){66             if(!attach.isEmpty()){//判断文件是否为空67                 File file = new File(realPath+"/"+attach.getOriginalFilename());68                 try {69                     //FileUtils.copyInputStreamToFile(attach.getInputStream(), file);70                     //FileUtils.writeByteArrayToFile(file, attach.getBytes());71                     attach.transferTo(file);72                 } catch (Exception e) {73                     // TODO Auto-generated catch block74                     e.printStackTrace();75                 }76             }77         }78         userList.put(user.getUserName(), user);79         return "redirect:/user/userlist";80     }81 82 83 84 85 86 4.在add.jsp中的form表单中添加,如果是多文件上传就按照需求添加87     <!--单文件上传文件-->88     Attach:<input type="file" name="attach"/>89 90         <!--多文件上传文件,注意:attachs-->91             Attach1:<input type="file" name="attachs"/><br/>92         Attach2:<input type="file" name="attachs"/><br/>93         Attach3:<input type="file" name="attachs"/><br/>94         Attach4:<input type="file" name="attachs"/><br/>

 

SpringMVC文件上传