1.web.xml(添加)
<web-app>
<!--1.在web-app标签下添加命名空间-->
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!--2.在<servlet>中添加 -->
<multipart-config></multipart-config>
2.springmvc-servlet.xml (添加)
<bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver" id="multipartResolver"/>
3.controller层
public String tooAdd(Book book, HttpServletRequest request, Model model, MultipartFile photox) throws IOException {
if (!photox.isEmpty()){
String realPath = request.getRealPath("/resources/");
System.out.println(realPath);
String ext=photox.getOriginalFilename().substring(photox.getOriginalFilename().lastIndexOf("."));
String newFileName= UUID.randomUUID()+ext;
System.out.println(newFileName);
photox.transferTo(new File(realPath+newFileName));
book.setPhoto(newFileName);
System.out.println(book);
}
System.out.println(book);
int ret=bookServer.add(book);
if (ret>0){
return "redirect:/book/select";
}
List<Type> types=bookServer.selectTypeAll();
model.addAttribute("types",types);
return "book/add1";
}