zoukankan      html  css  js  c++  java
  • [六]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";

      }

  • 相关阅读:
    如何删除日志?
    sql lock
    生成DAL
    字符串ID替换
    精典SQL:分组合并列值
    SQL Server2005 XML数据类型基础
    Buckup
    SQL试题
    SQL处理表重复记录
    Left Join 中on与where的区别
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/5125907.html
Copyright © 2011-2022 走看看