[六]SpringMvc学习-文件上传

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";

  }

posted on 2016-01-12 22:56  Simle  阅读(196)  评论(0编辑  收藏  举报