java文件夹上传,保留文件夹结构

需求: 产品要求可以上传文件夹,文件夹下包含其他文件夹

前端上传文件夹,可以把文件以及所在文件所在文件夹信息传到后端

1.前端设置

需要设置 webkitdirectory enctype = multipart/form-data




    
    文件夹上传


2.后端代码

service impl 代码

import cn.hutool.core.util.StrUtil;
import com.example.folder.upload.entity.TreeNodeDTO;
import com.example.folder.upload.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class FileServiceImpl implements FileService {
    /**
     * 文件夹上传
     *
     * @param files
     */
    public List folderUpload(MultipartFile[] files) {
        List treeNodeDTOList = new ArrayList<>();
        if (files == null || files.length == 0) {
            return new ArrayList<>();
        }
        String firstFolder = null;
        for (MultipartFile file : files) {
            TreeNodeDTO treeNodeDTO = new TreeNodeDTO();
            String filePath = file.getOriginalFilename();
            if (filePath.lastIndexOf(StrUtil.C_SLASH) > 0) {
                String dirPath = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(StrUtil.C_SLASH));
                // 设置文件所在目录的全目录路径
                treeNodeDTO.setFolderFullName(dirPath);
                String[] split = dirPath.split(StrUtil.SLASH);
                if (split != null) {
                    String folder = split[split.length - 1];
                    // 文件所在当前目录名称
                    treeNodeDTO.setFolderName(folder);
                }
                if (firstFolder == null) {
                    firstFolder = split[0];
                }
            }
            int index = filePath.lastIndexOf(StrUtil.C_SLASH);
            String fileName = filePath.substring(index + 1);
            // 文件名称
            treeNodeDTO.setName(fileName);
            treeNodeDTOList.add(treeNodeDTO);
        }
        // 判断是否存在一级目录
        List fullFolderList = treeNodeDTOList.stream().map(TreeNodeDTO::getFolderFullName).collect(Collectors.toList());
        if (firstFolder != null && !fullFolderList.contains(firstFolder)) {
            TreeNodeDTO treeNodeDTO = new TreeNodeDTO();
            treeNodeDTO.setFolderName(firstFolder);
            treeNodeDTO.setFolderFullName(firstFolder);
            treeNodeDTOList.add(treeNodeDTO);
        }
        treeNodeDTOList.forEach(treeNodeDTO -> {
            log.info("文件名:{},目录全路径:{},当前目录:{}",treeNodeDTO.getName(),treeNodeDTO.getFolderFullName(),treeNodeDTO.getFolderName());
        });
        return treeNodeDTOList;
    }

}

service 代码

import com.example.folder.upload.entity.TreeNodeDTO;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

public interface FileService {
    /**
     * 文件夹上传
     * @param files
     */
    List folderUpload(MultipartFile[] files);
}

3.controller

import com.example.folder.upload.entity.TreeNodeDTO;
import com.example.folder.upload.service.FileService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService fileService;

    @RequestMapping(value = "/folderUpload", method = RequestMethod.POST)
    public List uploadFolder(MultipartFile[] multipartFiles) {
        return fileService.folderUpload(multipartFiles);
    }
}

4. treeNodeDTO 

import lombok.Data;

@Data
public class TreeNodeDTO {
    /**
     * 文件名
     */
    private String name;
    /**
     * 最后一级文件夹名称
     */
    private String folderName;
    /**
     * 全路径文件夹
     */
    private String folderFullName;
}

5.效果图

java文件夹上传,保留文件夹结构_第1张图片 6. github代码地址

GitHub - katriina-tavi/folder-upload: 文件夹上传

你可能感兴趣的:(java,文件夹上传)