Java Web(SpringBoot 2.x)上传文件夹,完整代码

2019-05-27 09:08:40 浏览数 (1)

1、pom.xml

代码语言:javascript复制
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

2、SpringBootApplication

代码语言:javascript复制
package cn.hadron;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import javax.servlet.MultipartConfigElement;

@SpringBootApplication
public class DbApplication {

    public static void main(String[] args) {
        SpringApplication.run(DbApplication.class, args);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大   KB,MB
        factory.setMaxFileSize("100MB");
        /// 设置总上传数据总大小
        factory.setMaxRequestSize("500MB");
        return factory.createMultipartConfig();
    }

}

3、controller

代码语言:javascript复制
package cn.hadron.controller;

importcn.hadron.db.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping(value = "/file")
public class FileController {
    @RequestMapping(value="/upload.do",method= RequestMethod.POST)
    public String upload(@RequestParam("dir") MultipartFile[] dir) {
        System.out.println("上传文件夹...");
        File file;
        String fileName="";
        String filePath="";
        for (MultipartFile f : dir) {
            fileName=f.getOriginalFilename();
            String type=f.getContentType();
            System.out.println("n" fileName " ," type);
            filePath="D:\upload\" fileName.substring(0,fileName.lastIndexOf("/"));
            if(!FileUtil.isDir(filePath)){
                FileUtil.makeDirs(filePath);
            }
            file = new File("D:\upload\"   fileName);
            try {
                file.createNewFile();
                //将上传文件保存到一个目标文件中
                f.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "filelist";
    }
}
代码语言:javascript复制
package cn.hadron.db.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;



/**
 * 文件操作工具类
 *
 * @author cyq
 *
 */
public class FileUtil {

	public static boolean isFile(String filepath) {
		File f = new File(filepath);
		return f.exists() && f.isFile();
	}

	public static boolean isDir(String dirPath) {
		File f = new File(dirPath);
		return f.exists() && f.isDirectory();
	}

	/**
	 * 创建多级目录
	 * @param path
	 */
	public static void makeDirs(String path) {
		File file = new File(path);
		// 如果文件夹不存在则创建
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}else {
			System.out.println("创建目录失败:" path);
		}
	}
}

4、上传页面

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/db/file/upload.do" enctype="multipart/form-data" method="post">
    <input id="dir" type="file" name="dir" webkitdirectory mozdirectory/>
    <input id="uploadDir" type="submit" value="提交文件夹">
</form>
</body>
</html>

0 人点赞