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

话不多说,直接上代码

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;
    }

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值