zoukankan      html  css  js  c++  java
  • Spring MVC文件上传

    单文件上传

      所需依赖

      <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
        </dependency>
        <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>1.2</version>
        </dependency>

      大配置文件

    <!--文件上传解析器-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!--编码-->
            <property name="defaultEncoding" value="UTF-8"/>
            <!--文件总大小-->
            <property name="maxInMemorySize" value="5000000"/>
            <!--单文件大小-->
            <property name="maxUploadSizePerFile" value="200000"/>
        </bean>

      表单

    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <form action="/file/fileone" method="post" enctype="multipart/form-data">
            <input type="file" name="file"><br>
            <input type="password" name="upwd"><br>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

      控制器

    @RequestMapping("/fileone")
        public ModelAndView fileone(HttpSession session, MultipartFile file,String upwd) throws IOException {
            ModelAndView mv=new ModelAndView();
            System.out.println(upwd);
                //文件是否为空
                if(!file.isEmpty()){
                    //获取文件名称
                    String filename=file.getOriginalFilename();
                    //获取文件上传路径
                    String realPath = session.getServletContext().getRealPath("/WEB-INF/file");
                    //创建文件对象
                    File file1=new File(realPath,filename);
                    //上传文件
                    file.transferTo(file1);
                }
            mv.setViewName("myform");
            return mv;
        }

     多文件上传

      修改控制器

     @RequestMapping("/fileone1")
        public ModelAndView fileone1(HttpSession session, MultipartFile[] file,String upwd) throws IOException {
            ModelAndView mv=new ModelAndView();
            System.out.println(upwd);
            for (MultipartFile files:file){
                //是否为空
                if(!files.isEmpty()){
                    //获取文件名称
                    String filename=files.getOriginalFilename();
                    //获取文件上传路径
                    String realPath = session.getServletContext().getRealPath("/WEB-INF/file");
                    //创建文件对象
                    File file1=new File(realPath,filename);
                    //上传文件
                    files.transferTo(file1);
                }
            }
            mv.setViewName("myform");
            return mv;
        }

      修改表单

    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <form action="/file/fileone" method="post" enctype="multipart/form-data">
            <input type="file" name="file"><br>
            <input type="file" name="file"><br>
            <input type="file" name="file"><br>
            <input type="file" name="file"><br>
            <input type="password" name="upwd"><br>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

  • 相关阅读:
    MATLAB 显示精度 用法设置以及实例
    matlab MCR路径
    fmincon如何使非线性约束函数写成匿名函数的形式
    Matlab强行终止程序执行
    Matlab绘制图像后在指定点绘制坐标线以及标注变量
    c#各种变量的总结
    Matlab绘制图像及图像的处理
    MATLAB 嵌套函数,子函数,私有函数,重载函数
    Matlab 接受字符串并转为符号表达式,inline函数,匿名函数形式的方法汇总
    MATLAB 函数编写方法, 浅析MATLAB中的内联函数、匿名函数和函数函数
  • 原文地址:https://www.cnblogs.com/whtt/p/11835306.html
Copyright © 2011-2022 走看看