概述
话不多说,直接上代码
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 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 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
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
最后
以上就是结实路人为你收集整理的java 根据路径获取文件_根据路径获取该路径下文件夹和文件的全部内容,希望文章能够帮你解决java 根据路径获取文件_根据路径获取该路径下文件夹和文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复