欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

根据路径获取该路径下文件夹和文件

程序员文章站 2022-03-19 15:56:16
话不多说,直接上代码import lombok.Data;@Datapublic class Tree { private Integer id; private String name;//文件夹或者文件名称 private String path;//全路径,或则部分路径,自己决定 private Integer parentId;//父节点id public Tree(Integer id, String name, String path, Int...

话不多说,直接上代码

import lombok.Data;

@Data
public class Tree {

    private Integer id;
    private String name;//文件夹或者文件名称
    private String path;//全路径,或则部分路径,自己决定
    private Integer parentId;//父节点id

    public Tree(Integer id, String name, String path, Integer parentId) {
        this.id = id;
        this.name = name;
        this.path = path;
        this.parentId = parentId;
    }
}
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/tree")
@Api(tags = "获取本地文件夹数据组装树形结构")
public class FileController {

    @Autowired
    private FileService service;

    private static List<Tree> list = new ArrayList<>();//用来存放数据
    private static Integer id = 0;//因为测试使用,当初主键id来用

    @GetMapping
    @ApiImplicitParam(name = "fileName", value = "文件夹名称", dataType = "String", required = true)
    public Result getTree(String fileName) {
        int parentid = 0;  //初始化父节点id
        List trees = new ArrayList<>();
        try {
            file(fileName, parentid);
            trees = service.getList(FileController.list, parentid);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return ResultUtils.success(trees);
    }


    public static void file(String filepath, int parentid) throws FileNotFoundException {
        File file = new File(filepath);
        //1.判断文件
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在");
        }
        //2.是文件该怎么执行
        if (file.isFile()) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            Tree tree = new Tree(id++, name, path, parentid);
            list.add(tree);
            return;
        }
        //3.获取文件夹路径下面的所有文件递归调用;
        if (file.isDirectory()) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            Tree tree = new Tree(id++, name, name, parentid);
            list.add(tree);
            String[] list = file.list();
            for (int i = 0; i < list.length; i++) {
                String s = list[i];
                String newFilePath = path + "\\" + s;//根据当前文件夹,拼接其下文文件形成新的路径
                file(newFilePath, tree.getId());
            }
        }
    }
}
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FileServiceImpl implements FileService {

    @Override
    public List getList(List<Tree> list, int parentid) {
        List listTree = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Tree tree = list.get(i);
            if (tree.getParentId() == parentid) {
                Map map = new HashMap<>();
                map.put("name", tree.getName());
                if (tree.getPath().contains(".pdf")||tree.getPath().contains(".PDF")
                        ||tree.getPath().contains(".jpg")||tree.getPath().contains(".JPG")
                        ||tree.getPath().contains(".doc")||tree.getPath().contains(".gim")) {
                    map.put("url", tree.getPath());
                    listTree.add(map);
                } else {
                    list.remove(i);
                    map.put("child", getList(list, tree.getId()));
                    listTree.add(map);
                }
            }
        }
        return listTree;
    }

}

本文地址:https://blog.csdn.net/bc_520/article/details/111839724

相关标签: windows