springmvc图片上传

 1.需要添加文件上传的jar

 2.在tomcat上面设置虚拟路径

 

 

    <form id="itemForm" action="${pageContext.request.contextPath }/editItemsSubmit.action" enctype="multipart/form-data" method="post" >
<input type="hidden" name="id" value="${itemsCustom.id }"/>
<input type="hidden" name="pic" value="${itemsCustom.pic }"/>          //图片信息隐藏域
修改商品信息:
<table width="100%" border=1>
<tr>
    <td>商品名称</td>
    <td><input type="text" name="name" value="${itemsCustom.name }"/></td>
</tr>
<tr>
    <td>商品价格</td>
    <td><input type="text" name="price" value="${itemsCustom.price }"/></td>
</tr>
<tr>
    <td>商品生产日期</td>
    <td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
<tr>
    <td>商品图片</td>
    <td>
        <c:if test="${itemsCustom.pic !=null}">
            <img src="/pic/${itemsCustom.pic}" width=100 height=100/>
            <br/>
        </c:if>
        <input type="file"  name="items_pic"/>
    </td>

</tr>
<tr>
    <td>商品简介</td>
    <td>
    <textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
    </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"/>
<input type="button" value="返回" οnclick="back()"/>
</td>
</tr>
</table>
</form>

 

 

 

 

 

/**
     * 商品信息修改提交
     * @param request
     * @param id
     * @param itemsCustom
     * @return
     * @throws Exception
     */
    //在需要校验的pojo前面加@Validated,在需要校验的pojo后面添加BindingResult BindingResult接收校验出错信息
    //注意:@Validated和BindingResult是配对出现的,并且 形参顺序是固定的(一前一后)
    //value={ValidGroup2.class}指定使用ValidGroup2分组的校验
    //@ModelAttribute("itemsCustom")可以指定pojo回显到页面在request中的key
    //MultipartFile items_pic //接收商品图片
    @RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(Model model,HttpServletRequest request,
            @RequestParam(value="id",required=true) Integer id,
            @ModelAttribute("itemsCustom")
    @Validated(value={ValidGroup2.class}) ItemsCustom itemsCustom,
    BindingResult bindingResult,
    MultipartFile items_pic //接收商品图片
            )throws Exception{
        //获取校验信息
        if(bindingResult.hasErrors()) {

            List<ObjectError> allErrors = bindingResult.getAllErrors();
            //将错误信息传到页面
            model.addAttribute("allErrors", allErrors);
            //出错重新到修改页面
            /**
             * 可以直接使用model将提交失败的pojo回显到页面
             * model.addAttribute("items", itemsCustom);
             */
            return "items/editItems";
        }
        /**
         * 上传图片
         */
        //图片的原始名称
        String originalFilename = items_pic.getOriginalFilename();
        if(items_pic!=null&&originalFilename!=null&&originalFilename.length()>0) {
            //存储图片的物理路径
            String pic_path = "D:\\MyPicture\\";
            //新的图片名称
            String newFile_Name = UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
            //新图片
            File newFile = new java.io.File(pic_path+newFile_Name);
            //将内存中的数据写入磁盘
            items_pic.transferTo(newFile);
            //将新图片名称写到items_Custom中
            itemsCustom.setPic(newFile_Name);
        }
        //调用service更新商品信息,页面需要将商品信息传到此方法
        itemsService.updateItems(id, itemsCustom);

        //重定向 url,地址栏变化
    return "redirect:queryItems.action";
 
        //      return "success"; 
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值