[六]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";

  }

上一篇:Markdown 学习资源


下一篇:【手记】解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题