首先要导入2个包(上传文件包和io的包)
配置文件spring-servlet.xml文件中
<!-- 配置文件上传MultipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5000000"></property> </bean>
如果id="multipartResolver"没传,就会报以下的错(小心小心)
Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
添加文件的显示页面add.sjp
a、enctype="multipart/form-data"这句就是告诉,要添加文件,很重要
b、 attach:<input type="file" name="attach"><br/>
<form:form modelAttribute="user" method="post" enctype="multipart/form-data"> ID:<form:input path="id"/><br/> userName:<form:input path="userName"/><form:errors path="userName"/><br/> password:<form:input path="password"/><form:errors path="password"/><br/> email:<form:input path="email"/><form:errors path="email"/><br/> attach:<input type="file" name="attach"><br/> <input type="submit" value="提交"/> </form:form>
修改添加的方法
a、指定路径
b、上传文件,就要文件保存的目标位置
@RequestMapping(value="/add",method=RequestMethod.POST) public String add(@Validated User user,BindingResult bindingResult,MultipartFile attach,HttpServletRequest request){ //上传文件就是要上传的路径所以就需要request对象 if(bindingResult.hasErrors()){ return "user/add"; } //指定上传位置 String uploadFilePath = request.getSession().getServletContext().getRealPath("statics/upload"); System.out.println(uploadFilePath); System.out.println(attach.getOriginalFilename());//原始名 System.out.println(attach.getContentType());//类型 System.out.println(attach.getName());//页面表单控件name的值 if(!attach.isEmpty()){ try { //"/" 更换成自动识别分隔符File.separator File saveFile = new File(uploadFilePath+File.separator+attach.getOriginalFilename()); //第一个是拿到输入流,第二个参数是保存到哪里 FileUtils.copyInputStreamToFile(attach.getInputStream(), saveFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //把user对象放进去key(id) userList.put(user.getId(), user); // return "user/userlist";//返回到逻辑视图名 //(添加后,不能把添加过的数据展示出来)服务器端行为 return "redirect:/user/list";//客户端重新发送请求(把添加进去的展示出来) }
多文件的上传
因为是多文件所以一定要加@RequestParam不加的话报如下异常
//Could not instantiate bean class [[Lorg.springframework.web.multipart.MultipartFile;]: No default constructor found;nested exception is java.lang //不能实例化bean类[[Lorg.springframework.web.multipart。MultipartFile;]:没有默认构造函数发现,嵌套异常. lang。
//页面500
//文件上传(多文件) @RequestMapping(value="/add",method=RequestMethod.POST) public String add(@Validated User user,BindingResult bindingResult,@RequestParam("attachs") MultipartFile[] attachs,HttpServletRequest request){ //上传文件就是要上传的路径所以就需要request对象 if(bindingResult.hasErrors()){ return "user/add"; } //指定上传位置 String uploadFilePath = request.getSession().getServletContext().getRealPath("statics/upload"); try { for(MultipartFile attach:attachs){ if(!attach.isEmpty()){ //"/" 更换成自动识别File.separator File saveFile = new File(uploadFilePath+File.separator+attach.getOriginalFilename()); //第一个是拿到输入流,第二个参数是保存到哪里 FileUtils.copyInputStreamToFile(attach.getInputStream(), saveFile); } } }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //把user对象放进去key(id) userList.put(user.getId(), user); // return "user/userlist";//返回到逻辑视图名 //(添加后,不能把添加过的数据展示出来)服务器端行为 return "redirect:/user/list";//客户端重新发送请求(把添加进去的展示出来) }
因为多文件是一个数组,attach:<input type="file" name="attachs"><br/>
这边的name要是attachs,否则就找不到
<form:form modelAttribute="user" method="post" enctype="multipart/form-data"> ID:<form:input path="id"/><br/> userName:<form:input path="userName"/><form:errors path="userName"/><br/> password:<form:input path="password"/><form:errors path="password"/><br/> email:<form:input path="email"/><form:errors path="email"/><br/> attach:<input type="file" name="attachs"><br/> attach1:<input type="file" name="attachs"><br/> attach2:<input type="file" name="attachs"><br/> <input type="submit" value="提交"/> </form:form>