创建FTP略,直接上代码
ftp:
server: 038.438.383.388
port: 21
username: 038
password:
#application.yml中添加以上配置
controller
@RestController
@RequestMapping("/ftpNodes")
@Api(value = "API - FtpNodeController", tags = "读取树")
public class FtpNodeController extends BaseController {
@ApiOperation(value = "获取树")
@PostMapping("/getFTPNodeListInfo")
@ApiImplicitParams({
@ApiImplicitParam(name = "dirName", value = "目录名称", dataType = "string", paramType = "query")
//,@ApiImplicitParam(name = "projectCode", value = "项目编码", dataType = "string", paramType = "query")
})
public Result getFTPNodeListInfo(String dirName){
List<Node> nodeList = FtpUtil.getTree("test/" + dirName);
return new Result(nodeList);
}
}
工具类
/**
* @描述: 生成目录树
* @作者: fan
* @日期: 2021/05/20 10:56
**/
public static List<Node> getTree(String path) {
FTPClient ftp = localConn();
if (ftp != null) {
getExchageDirLength(path, ftp);
String rootNodeName = path.substring(path.lastIndexOf("/") + 1);
List<Node> nodeList = new ArrayList<>();
Node rootNode = Node.getDirNode(getId(), rootNodeName, path,"-1");
nodeList.add(rootNode);
listTree(ftp, path, rootNode,nodeList);
return nodeList;
} else {
return null;
}
}
Node.java
import java.util.List;
public class Node {
//get,set略
private String id;
private String name;
private String path;
private Integer type;
private Integer treeLevel;
private String parentId;
private List<Node> children;
private Node(String id, String name, String path, Integer type, Integer treeLevel, String parentId) {
this.id = id;
this.name = name;
this.path = path;
this.type = type;
this.treeLevel = treeLevel;
this.parentId = parentId;
}
public static Integer getTreeLevel2(String path){
char[] chars = path.toCharArray();
int level = 0;
for(int i = 0; i < chars.length; i++){
if(chars[i] == '/'){
level++;
}
}
return level;
}
public static Node getDirNode(String id, String name, String path, String parentId) {
return new Node(id, name, path, 1, getTreeLevel(path),parentId);
}
public static Node getFileNode(String id, String name, String path, String parentId) {
return new Node(id, name, path, 2, getTreeLevel2(path),parentId);
}
}