首页 > 编程语言 > 详细

[六]SpringMvc学习-文件上传

时间:2016-01-13 00:20:20      阅读:311      评论:0      收藏:0      [点我收藏+]

1.单文件上传

  1.1修改配置文件

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="maxUploadSize" value="10000000"/>

  </bean>

  1.2添加两个jar包

    com.springsource.org.apache.commons.fileupload-1.2.0.jar

    com.springsource.org.apache.commons.io-1.4.0.jar

  示例代码:

  @RequestMapping("/upload")

  public String uploadFile(@RequestParam("file") MultipartFile file,HttpServletRequest request){

    String filePath = request.getServletContext.getRealPath("/");

    file.transferTo(new File(filePath+"upload/"+file.getOrigalName()));

    return "redirect:success.html";

  }

2.多文件上传

1.1修改配置文件  

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="UTF-8"/> 
    <property name="maxUploadSize" value="10000000"/>

  </bean>

  1.2添加两个jar包

    com.springsource.org.apache.commons.fileupload-1.2.0.jar

    com.springsource.org.apache.commons.io-1.4.0.jar

  示例代码:

  @RequestMapping("/upload")

  public String uploadFile(@RequestParam("file") MultipartFile[] files,HttpServletRequest request){

    String filePath = request.getServletContext.getRealPath("/");

    for(MultipartFile file : files){

      file.transferTo(new File(filePath+"upload/"+file.getOrigalName()));

    }

    return "redirect:success.html";

  }

[六]SpringMvc学习-文件上传

原文:http://www.cnblogs.com/luoxiaolei/p/5125907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!