SpringMvc上传文件

前台代码


注:/pic tomcat访问本地磁盘路径 (=x:xxx)
<tr>
<td>商品图片</td>
<td>
<c:if test="${对象类型.pic !=null}">
<img src="/pic/${对象类型.pic}" width=100 height=100/>
<br/>
</c:if>
<input type="file"  name="pictureFile"/> 
</td>
</tr>


后台代码

注:MultipartFile 的实现类需要在springMvc配置

   @RequestMapping("/item/updateItem.action")
   public ModelAndView updateItem(Items item,MultipartFile pictureFile) throws Exception{
//String name = pictureFile.getName(); 得到input name值
String name = pictureFile.getOriginalFilename();   得到上传文件的名字.
String ext = FilenameUtils.getExtension(name);     得到文件类型
String fileName=UUID.randomUUID()+"."+ext;
File targetFile = new File("F:/tomcat_picture/", fileName);
pictureFile.transferTo(targetFile);
item.setPic(fileName);
itemService.updateItem(item);
ModelAndView mav=new ModelAndView();
mav.setViewName("success");
return mav;
}

    

SpingMvc配置文件

<!-- 上传文件的实体类,id一定是multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值