1.在pom.xml中,maven坐标导入 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> 2.在springmvc和spring整合的.xml文件中配置bean标签 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为1MB --> <property name="maxUploadSize"> <value>1048576</value> </property> </bean> 3.jsp页面 3.1form表单属性设置 <form role="form" style="position: static;" method="post" enctype="multipart/form-data" action="houseload.action" enctype="multipart/form-data"> 3.2 form表单中input标签name属性的值是pojo实体类的属性,以便Controller可以用实体类对象接受 4.Controller的书写 @RequestMapping("/houseload") public String houseLoad(House house,HttpSession session,MultipartHttpServletRequest request) throws IOException{ // 获得文件: MultipartFile file = request.getFile("file"); String filename = file.getOriginalFilename(); int index=filename.lastIndexOf("."); //取文件名后缀 String filesub=filename.substring(index); //生成新的文件名 filename=UUID.randomUUID().toString()+System.currentTimeMillis()+filesub; FileUtils.writeByteArrayToFile(new File("d:\\houseimgs\\"+filename), file.getBytes()); house.setHouseImages(filename); //从session中获取用户ID User user=(User)session.getAttribute("user"); String ownerId=user.getUserId(); //保存房屋信息 houseService.saveHouse(house,ownerId); return "/index"; }