• springMVC实现 MultipartFile 多文件上传


    1、Maven引入所需的 jar 包(或自行下载)

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

    2、配置 spring 文件

      <!-- 多部分文件上传 -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
            <property name="defaultEncoding" value="utf-8"></property>   
            <property name="maxUploadSize" value="10485760000"></property>  
            <property name="maxInMemorySize" value="40960"></property>  
       </bean> 

    3、form 添加 enctype="multipart/form-data"

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h2>上传多个文件 实例</h2>  
        <form action="/upload/filesUpload" method="post"  enctype="multipart/form-data">  
            <p>选择文件:<input type="file" name="files"></p>
            <p>选择文件:<input type="file" name="files"></p>
            <p><input type="submit" value="提交"></p>
        </form>  
    </body>
    </html>

    5、controller 部分

    import java.io.File;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    
    @Controller
    @RequestMapping("/upload")
    public class UploadController {
        //通过Spring的autowired注解获取spring默认配置的request  
    
        /*** 
         * 保存文件 
         * @param file 
         * @return 
         */  
        private boolean saveFile(MultipartFile file, String path) {  
            // 判断文件是否为空  
            if (!file.isEmpty()) {  
                try {  
                    File filepath = new File(path);
                    if (!filepath.exists()) 
                        filepath.mkdirs();
                    // 文件保存路径  
                    String savePath = path + file.getOriginalFilename();  
                    // 转存文件  
                    file.transferTo(new File(savePath));  
                    return true;  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
            return false;  
        }  
    
        @RequestMapping("/filesUpload")  
        public String filesUpload(@RequestParam("files") MultipartFile[] files) { 
            String path = "E:/upload/";
            //判断file数组不能为空并且长度大于0  
            if(files!=null&&files.length>0){  
                //循环获取file数组中得文件  
                for(int i = 0;i<files.length;i++){  
                    MultipartFile file = files[i];  
                    //保存文件  
                    saveFile(file, path);  
                }  
            }  
            // 重定向  
            return "redirect:/list.html";  
        }  
    
    }

    运行如下:

  • 相关阅读:
    sublime设置node.js编译
    Markdown基本语法规范
    vue2.0学习笔记之组件
    使用let声明变量的理解
    vue2.0学习笔记之路由(二)路由嵌套+动画
    vue2.0学习笔记之路由(二)路由嵌套
    设置webstorm支持ES6语法
    vue2.0环境搭建
    bower包管理工具
    【转】jquery cookie操作
  • 原文地址:https://www.cnblogs.com/linnuo/p/7805334.html
Copyright © 2020-2023  润新知