0
点赞
收藏
分享

微信扫一扫

#yyds干货盘点#java获取文件树

1.文件树结构

@ApiModel("文件树")
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@Data
public class BaseTreeNodeVO {
@ApiModelProperty(value = "文件id", required = true)
protected Long id;

@ApiModelProperty(value = "父id", required = true)
protected Long parentId;

@ApiModelProperty(value = "文件夹标识", required = true)
protected Boolean isDirectory;

@ApiModelProperty(value = "文件名", required = true)
private String name;

@ApiModelProperty(value = "子节点", required = true)
protected List<BaseTreeNodeVO> children = new ArrayList<>();

public void appendChild(BaseTreeNodeVO organTree){
this.children.add(organTree);
}
}

2.获取文件树

public BaseTreeNodeVO getFileTree(String filePath){

long parentId = 1; //初始化父节点id
long id = 1; //初始化父节点id
List<FileTree> list = new ArrayList<>();
try {
FileUtil.file(id,filePath,parentId,list);

} catch (FileNotFoundException e) {
e.printStackTrace();
}

BaseTreeNodeVO bt = FileUtil.assembleTree(list);
bt = FileUtil.sort(bt);
LOG.info(LogProperty.LOGTYPE_DETAIL, JSON.toJSONString(bt));
return bt;
}

3.转换为list

public static List<FileTree> file(Long id,String filePath,long parentId, List<FileTree> treeList) throws FileNotFoundException {
File file = new File(filePath);

//1.判断文件
if(!file.exists()){
throw new FileNotFoundException("文件不存在");
}

//3.获取文件夹路径下面的所有文件递归调用;
if(file.isDirectory()){
String path = file.getAbsolutePath();
String name = file.getName();
FileTree fileTree = new FileTree(id++,name,path,parentId,true);
treeList.add(fileTree);
String[] list = file.list();
for (int i = 0;i<list.length;i++){
String s = list[i];
String newFilePath = path+ File.separator +s;//根据当前文件夹,拼接其下文文件形成新的路径
file(id++,newFilePath,fileTree.getId(),treeList);
}
}
//2.是文件该怎么执行
if(file.isFile()){
LOG.info(LogProperty.LOGTYPE_DETAIL, "is file");
String name = file.getName();
String path = file.getAbsolutePath();
FileTree fileTree = new FileTree(id++,name,path,parentId,false);
treeList.add(fileTree);
return treeList;
}

return treeList;
}

4.转换为tree

public static <T extends BaseTreeNodeVO> T assembleTree(List<T> list) {

if (list.isEmpty()) {
return null;
}

//取出根节点
T root = list.get(0);
//临时存放所有节点
Map<Long, T> nodeMap = new HashMap<>();
list.stream().forEach(o -> nodeMap.put(o.getId(), o));
//找出节点关系
list.subList(1, list.size()).stream().forEach(m -> nodeMap.get(m.getParentId()).appendChild(m));
list.stream().sorted(Comparator.comparing(BaseTreeNodeVO::getIsDirectory)).collect(Collectors.toList());
nodeMap.clear();
return root;
}

5.排序

public static BaseTreeNodeVO sort(BaseTreeNodeVO bt) {

List<BaseTreeNodeVO> list = bt.getChildren();
while (!list.isEmpty()) {
List<BaseTreeNodeVO> dir = list.stream().filter(a -> a.getIsDirectory()).collect(Collectors.toList());
Collections.sort(dir, Comparator.comparing(BaseTreeNodeVO::getName));
List<BaseTreeNodeVO> file = list.stream().filter(a -> !a.getIsDirectory()).collect(Collectors.toList());
Collections.sort(file, Comparator.comparing(BaseTreeNodeVO::getName));
for (BaseTreeNodeVO btChildren : dir) {
sort(btChildren);
}
dir.addAll(file);
bt.setChildren(dir);
return bt;
}
return bt;
}
}

6.接口返回示例

#yyds干货盘点#java获取文件树_初始化

举报

相关推荐

0 条评论