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

      }

  • 相关阅读:
    WAF与IPS的区别总结
    web后门排查与高效分析web日志技巧
    如何做一名好的web安全工程师?
    从“黑掉Github”学Web安全开发
    DNS劫持
    万网上如何将IP和申请的域名绑定
    如何申请网站域名
    什么是域名?什么网站名?什么是URL?
    myeclipse svn 插件去除已经保存的密码方法
    SVN中检出 和 导出 的区别
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/5125907.html
Copyright © 2011-2022 走看看