使用SpringMVC框架实现文件上传

  1.配置上传文件解释器

(由于springMVC文件上传功能引用了commons-fileupload组件,所以记得导入commons-fileupload和commons-io包)
  <!—配置文件上传解释器 -->   <mvc:annotation-driven></mvc:annotation-driven>   <bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   <property name="defaultEncoding" value="UTF-8"></property>   <property name="maxInMemorySize" value="512000000" ></property>   <property name="maxUploadSize" value="20000000"></property>   </bean>
  
  2.在Controller层编写映射方法  

  @RequestMapping(value="upload")
   public String upload(MultipartFile file) throws Exception{
   File destfile = new File("D:/dir/" + file.getOriginalFilename());
   file.transferTo(destfile);
   return "/upload.jsp";
   }

   

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

  
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄