上传文件夹(多层级文件)到服务器

问题:提供在HTML中上传文件夹到服务器的指定文件夹下
实现思路:将层级文件打成zip压缩包,上传到服务器后解压缩至指定文件夹

项目所需依赖

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.18</version>
        </dependency>

代码



import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;

/**
 * @author Czw
 * @Description 管理员Controller
 * @Date 2019/4/17 0017 下午 3:01
 */
@Slf4j
@RestController
public class ArchiveController {
    
    

    @PostMapping("/archive")
    public String archive(MultipartFile file) {
    
    
        try {
    
    
            String path = "/opt/nginx/html/u3d/catalog1";
            unzip(file.getInputStream(), path);
            log.info("***解压缩完成***");
        } catch (IOException e) {
    
    
            log.error("***解压压缩包出错***");
            e.printStackTrace();
        }
        return "success";
    }


    /**
     * 压缩文件夹到指定输出流中,可以是本地文件输出流,也可以是web响应下载流
     *
     * @param srcDir       源文件夹
     * @param outputStream 压缩后文件的输出流
     * @throws IOException IO异常,抛出给调用者处理
     * @auther CZW
     */
    public static void zip(String srcDir, OutputStream outputStream) throws IOException {
    
    
        try (
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                ArchiveOutputStream out = new ZipArchiveOutputStream(bufferedOutputStream);
        ) {
    
    
            Path start = Paths.get(srcDir);
            Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
    
    

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
    
    
                    ArchiveEntry entry = new ZipArchiveEntry(dir.toFile(), start.relativize(dir).toString());
                    out.putArchiveEntry(entry);
                    out.closeArchiveEntry();
                    return super.preVisitDirectory(dir, attrs);
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    
    
                    try (
                            InputStream input = new FileInputStream(file.toFile())
                    ) {
    
    
                        ArchiveEntry entry = new ZipArchiveEntry(file.toFile(), start.relativize(file).toString());
                        out.putArchiveEntry(entry);
                        IOUtils.copy(input, out);
                        out.closeArchiveEntry();
                    }
                    return super.visitFile(file, attrs);
                }

            });

        }
    }

    /**
     * 解压zip文件到指定文件夹
     *
     * @param zipFileName 源zip文件路径
     * @param destDir     解压后输出路径
     * @throws IOException IO异常,抛出给调用者处理
     * @auther CZW
     */
    public static void unzip(String zipFileName, String destDir) throws IOException {
    
    
        try (
                InputStream inputStream = new FileInputStream(zipFileName);
        ) {
    
    
            unzip(inputStream, destDir);
        }

    }

    /**
     * 从输入流中获取zip文件,并解压到指定文件夹
     *
     * @param inputStream zip文件输入流,可以是本地文件输入流,也可以是web请求上传流
     * @param destDir     解压后输出路径
     * @throws IOException IO异常,抛出给调用者处理
     * @auther CZW
     */
    public static void unzip(InputStream inputStream, String destDir) throws IOException {
    
    
        try (
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                ArchiveInputStream in = new ZipArchiveInputStream(bufferedInputStream);
        ) {
    
    
            ArchiveEntry entry;
            while (Objects.nonNull(entry = in.getNextEntry())) {
    
    
                if (in.canReadEntryData(entry)) {
    
    
                    File file = Paths.get(destDir, entry.getName()).toFile();
                    if (entry.isDirectory()) {
    
    
                        if (!file.exists()) {
    
    
                            file.mkdirs();
                        }
                    } else {
    
    
                        try (
                                OutputStream out = new FileOutputStream(file);
                        ) {
    
    
                            IOUtils.copy(in, out);
                        }
                    }
                } else {
    
    
                    System.out.println(entry.getName());
                }
            }
        }

    }

}

测试结果

使用postman对进口进行测试,结果如下
在这里插入图片描述
上传完成后可以看到服务器目录下确实有解压后的文件
在这里插入图片描述


不忘初心

猜你喜欢

转载自blog.csdn.net/qq_42910468/article/details/105278243