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g56z9WlbUV4EVN5uW9BYVOF0TdweEHUPxhZ09SnRyFupVOq221/cs759WGJ+dspffvcg+Je4UqkklIMO3y6EKsTG+sIPN5Y3o5ojcWoWCszKlmC4IjiJqxu2ux9LSyoQI6Wg8mYhS7O31QMNxoEblb/jpPewySqCPA9mRrLJUR0yNGguLo/Xif86V3AjAwtBm9wsOnb9ySTZYdd9gL/E/2pcBvbZRAwMiq0jHREjqbgZ2ouABye1NiwTMfQt2+poIm8+F6Bf1OytWCx22u1uu93rdHrdbr/XHfR6g37fHQyGrusNXc8bFotFpGQydVHv1Li5vJnUnMy9kqyyPfxTbS2BWZgI5zTyBi5WNpej2dvKwceC+AUqcL/xczXuRpTvQ0wKqnZQYmYgGxcYUJMkzWnqFDdczBfO1DDoKBEpwRLhzxEnwO3oYWK0qCVkaDn5DcsSOwp8r/KKh9cIM4tvZXZ50//VlwU+4GGqLIyBXjo+WB9tyFKWk6KRt62trc71dad9HSlch1W4gTd0t7a2jJsjAKRfdxiTCMvKhaAIYq3QyNvm5mb76ipWuC6mcJubm8uxNWXQS0vuMKRuBLFwNPL2uRnLsTU9hCFNmroRBEEsCjrOlCAIgkghJG8EQRBECvkfblQnzOr5hB8AAAAASUVORK5CYII=" alt="" />
配置文件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>