<!-- 配置多媒體文件解析器 -->web <!-- 文件上傳bean的id必須是multipartResolver-->spring <bean id="multipartResolver"數據庫 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">mvc <!-- 設置上傳文件的最大尺寸爲5MB -->app <property name="maxUploadSize">dom <value>5242880</value>spa </property>3d </bean>orm |
在方法的參數列表中使用MultiparFile 類型進行接收xml
@RequestMapping("/updateitem") public String updateItem(Items items,MultipartFile picture) throws Exception {
//爲每一個圖片生成一個新的不重複的文件名 String picName = UUID.randomUUID().toString().replaceAll("-","");
//獲取文件的擴展名 String oriName = picture.getOriginalFilename(); String extName = oriName.substring(oriName.lastIndexOf(".")); //保存文件到虛擬文件目錄下 picture.transferTo(new File("D:\\develop\\StSWorkespace\\tem\\images\\"+picName+extName)); //將文件名保存進數據庫中 items.setPic(picName+extName); itemService.updateItem(items); //返回成功頁面 return"forward:/item/itemEdit.action"; } |
Type 必須是file name 須要和參數列表中的形參一致
表單的enctype="multipart/form-data">,提交方式必須是POST