SSM实现文件上传
一、配置虛擬目錄
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/82840303
二、加入上傳文件需要的jar包
https://download.csdn.net/download/badao_liumang_qizhi/10686450
將兩個jar包放在lib下
三、配置上傳解析器
在springmvc.xml中配置文件上傳解析器
??
???? <!-- 上傳圖片配置實現類 Id 必須為multipartResolver--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 上傳圖片的大小?? B?? 5M? 1*1024*1024*5--><property name="maxUploadSize" value="5000000"/></bean>四、配置jsp頁面
1.加入文件上傳的input
<tr><td>商品圖片</td><td><c:if test="${item.pic !=null}"><img src="/img/${item.pic}" width=100 height=100/><br/></c:if><input type="file"? name="pictureFile"/></td></tr>2.設置表單支持文件上傳
<formid="itemForm"?action="${pageContext.request.contextPath }/updateitem.action" method="post" enctype="multipart/form-data">五、配置圖片上傳的action
?
?
//去修改頁面 入參 id@RequestMapping(value = "/itemEdit.action") //?public ModelAndView toEdit(@RequestParam(value = "id",required = false,defaultValue = "1") Integer idaaq,public ModelAndView toEdit(Integer id,HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model){?//查詢一個商品 //??Items items = itemService.selectItemsById(Integer.parseInt(id));Items items = itemService.selectItemsById(id);ModelAndView mav = new ModelAndView();//數據mav.addObject("item", items);mav.setViewName("editItem");return mav;}六、跳到jsp頁面
<!-- 上傳圖片是需要指定屬性 enctype="multipart/form-data" --><formid="itemForm"?action="${pageContext.request.contextPath }/updateitem.action" method="post" enctype="multipart/form-data"><input type="hidden" name="items.id" value="${item.id }" /> 修改商品信息:<table width="100%" border=1><tr><td>商品名稱</td><td><input type="text" name="items.name" value="${item.name }" /></td></tr><tr><td>商品價格</td><td><input type="text" name="items.price" value="${item.price }" /></td></tr><tr><td>商品圖片</td><td><c:if test="${item.pic !=null}"><img src="/img/${item.pic}" width=100 height=100/><br/></c:if><input type="file"? name="pictureFile"/></td></tr><tr><td>商品簡介</td><td><textarea rows="3" cols="30" name="items.detail">${item.detail }</textarea></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交" /></td></tr></table></form>七、效果展示
點擊上選擇文件--提交
點擊提交
jsp頁面效果
配置的虛擬路徑下
?
總結
- 上一篇: Eclipse导入项目后中文乱码
- 下一篇: webpack简介