依赖

<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.4</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>

节点对象

package per.qiao.utils.ftp;

/**
* Create by IntelliJ Idea 2018.2
*
* @author: qyp
* Date: 2019-07-19 22:14
*/ import lombok.Data; import java.util.List; @Data
public class Node { private enum TYPE {
DIR("DIR"),
FILE("FILE")
;
private String type;
private TYPE(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
} private String id;
private String name;
private String path;
private TYPE type; private List<Node> childList;
private Node() {} private Node(String id, String name, String path, TYPE type) {
this.id = id;
this.name = name;
this.path = path;
this.type = type;
} public static Node getDirNode(String id, String name, String path) {
return new Node(id, name, path, TYPE.DIR);
}
public static Node getFileNode(String id, String name, String path) {
return new Node(id, name, path, TYPE.FILE);
}
}

生成节点目录树结构

package per.qiao.utils.ftp;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID; /**
* Create by IntelliJ Idea 2018.2
*
* @author: qyp
* Date: 2019-07-19 21:27
*/ public class FtpUtils { private final static Logger logger = LoggerFactory.getLogger(FtpUtils.class); /**
* 本地连接
* @return
*/
public static FTPClient localConn() {
String server = "127.0.0.1";
int port = 21;
String username = "test";
String password = "test";
// path = "/FTPStation/"; FTPClient ftpClient = null;
try {
ftpClient = connectServer(server, port, username, password, "/");
} catch (IOException e) {
e.printStackTrace();
}
return ftpClient;
} /**
*
* @param server
* @param port
* @param username
* @param password
* @param path 连接的节点(相对根路径的文件夹)
* @return
*/
public static FTPClient connectServer(String server, int port, String username, String password, String path) throws IOException {
path = path == null ? "" : path;
FTPClient ftp = new FTPClient(); //下面四行代码必须要,而且不能改变编码格式,否则不能正确下载中文文件
// 如果使用serv-u发布ftp站点,则需要勾掉“高级选项”中的“对所有已收发的路径和文件名使用UTF-8编码”
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
ftp.configure(conf); // 判断ftp是否存在
ftp.connect(server, port);
ftp.setDataTimeout(2 * 60 * 1000);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
System.out.println(server + "拒绝连接");
}
//登陆ftp
boolean login = ftp.login(username, password);
if (logger.isDebugEnabled()) {
if (login) {
logger.debug("登陆FTP成功! ip: " + server);
} else {
logger.debug("登陆FTP失败! ip: " + server);
}
} //根据输入的路径,切换工作目录。这样ftp端的路径就可以使用相对路径了
exchageDir(path, ftp); return ftp;
} /**
* 切换目录 返回切换的层级数
* @param path
* @param ftp
* @return 切换的层级数
* @throws IOException
*/
private static int exchageDir(String path, FTPClient ftp) {
// 切换的次数(层级),方便回退
int level = 0; try {
if (StringUtils.isNotBlank(path)) {
// 对路径按照 '/' 进行切割,一层一层的进入
String[] pathes = path.split("/");
for (String onepath : pathes) {
if (onepath == null || "".equals(onepath.trim())) {
continue;
}
//文件排除
if (onepath.contains(".")) {
continue;
}
boolean flagDir = ftp.changeWorkingDirectory(onepath);
if (flagDir) {
level ++;
logger.info("成功连接ftp目录:" + ftp.printWorkingDirectory());
} else {
logger.warn("连接ftp目录失败:" + ftp.printWorkingDirectory());
}
}
}
} catch (IOException e) {
logger.error("切换失败, 路径不存在");
e.printStackTrace();
throw new IllegalArgumentException("切换失败, 路径不存在");
}
return level;
} /**
* 生成目录树
* @return
*/
public static Node getTree(String path) {
FTPClient ftp = localConn();
exchageDir(path, ftp);
String rootNodeName = path.substring(path.lastIndexOf("/") + 1);
Node rootNode = Node.getDirNode(getId(), rootNodeName, path);
listTree(ftp, path, rootNode);
return rootNode;
} /**
* 遍历树结构
* @param ftp
* @param rootPath
* @param parentNode
*/
private static void listTree(FTPClient ftp, String rootPath, Node parentNode) { try {
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles.length <= 0) {
return;
}
for (FTPFile f : ftpFiles) {
List<Node> childList = parentNode.getChildList();
if (childList == null) {
childList = new ArrayList<>();
parentNode.setChildList(childList);
}
Node currentNode = null;
if (f.isDirectory()) {
currentNode = Node.getDirNode(getId(), f.getName(), rootPath + File.separator + f.getName());
if (ftp.changeWorkingDirectory(f.getName()) ) {
if (logger.isDebugEnabled()) {
logger.debug("进入:", ftp.printWorkingDirectory());
}
listTree(ftp, rootPath + File.separator + f.getName(), currentNode);
}
ftp.changeToParentDirectory();
if (logger.isDebugEnabled()) {
logger.debug("退出: {}", ftp.printWorkingDirectory());
}
} else {
currentNode = Node.getFileNode(getId(), f.getName(), rootPath + File.separator + f.getName());
}
childList.add(currentNode);
}
} catch (IOException e) {
e.printStackTrace();
logger.error("路径不存在");
} } private static String getId() {
return UUID.randomUUID().toString().replaceAll("-", "");
} public static void main(String[] args) {
Node rootNode = getTree("/CAD/第一层");
System.out.println(rootNode);
} }

生成ftp文件的目录树的更多相关文章

  1. python生成指定文件夹目录树

    # -*- coding: utf-8 -*- import sys from pathlib import Path class DirectionTree(object): "" ...

  2. C#实现生成Markdown文档目录树

    前言 之前我写了一篇关于C#处理Markdown文档的文章:C#解析Markdown文档,实现替换图片链接操作 算是第一次尝试使用C#处理Markdown文档,然后最近又把博客网站的前台改了一下,目前 ...

  3. 使用tree命令导出文件夹/文件的目录树

    前提:己安装扩展: 介绍: TREE [drive:][path] [/F] [/A] /F   显示每个文件夹中文件的名称.(带扩展名)   /A   使用 ASCII 字符,而不使用扩展字符. t ...

  4. paip.tree 生成目录树到txt后的折叠查看

    paip.tree 生成目录树到txt后的折叠查看 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.ne ...

  5. c# 表达式目录树拷贝对象(根据对象类型动态生成表达式目录树)

    表达式目录树,在C#中用Expression标识,这里就不介绍表达式目录树是什么了,有兴趣可以自行百度搜索,网上资料还是很多的. 这里主要分享的是如何动态构建表达式目录树. 构建表达式目录树的代码挺简 ...

  6. Python中的文件和目录操作实现

    Python中的文件和目录操作实现 对于文件和目录的处理,虽然可以通过操作系统命令来完成,但是Python语言为了便于开发人员以编程的方式处理相关工作,提供了许多处理文件和目录的内置函数.重要的是,这 ...

  7. PHP生成压缩文件开发实例

    大概需求: 每一个订单都有多个文件附件,在下载的时候希望对当前订单的文件自动打包成一个压缩包下载 细节需求:当前订单号_年月日+时间.zip  例如: 1.生成压缩文件,压缩文件名格式: 2.压缩文件 ...

  8. Python之文件与目录

    file 通常建议使用open()打开文件,file用于类型判断 如果要把数据写到磁盘上,除调用flush()外,还得用sync(),以确保数据从系统缓冲区同步到磁盘.close()总是会调用这两个方 ...

  9. WINDOWS 的 MKLINK : 硬链接,符号链接 : 文件符号链接, 目录符号链接 : 目录联接

    玩转WIN7的MKLINK 引言: 换了新电脑,终于再次使用上啦WIN7 ,经过一个周每天重装N次系统,... ... ... ... 在xp系统下,junction命令要用微软开发的小程序 junc ...

随机推荐

  1. 范仁义html+css课程---1、html基本结构

    范仁义html+css课程---1.html基本结构 一.总结 一句话总结: html标签中包含head标签和body标签,head标签里面主要写用户不可见的内容,比如字符集编码,body标签里面主要 ...

  2. 类别不平衡问题之SMOTE算法(Python imblearn极简实现)

    类别不平衡问题类别不平衡问题,顾名思义,即数据集中存在某一类样本,其数量远多于或远少于其他类样本,从而导致一些机器学习模型失效的问题.例如逻辑回归即不适合处理类别不平衡问题,例如逻辑回归在欺诈检测问题 ...

  3. 10分钟彻底理解Redis持久化和主从复制

    在这篇文章,我们一起了解 Redis 使用中非常重要的两个机制:Reids 持久化和主从复制. 什么是 Redis 持久化? Redis 作为一个键值对内存数据库(NoSQL),数据都存储在内存当中, ...

  4. iTunes Connect上传APP屏幕快照图片失败 - 您必须上传有效的屏幕快照。

    您必须上传有效的屏幕快照. 原因很简单:这个屏幕快照 要用 iPhone截屏才可以,你自已随便在电脑上截个图肯定不行 //--------------------------------------- ...

  5. (转)RL — Policy Gradient Explained

    RL — Policy Gradient Explained 2019-05-02 21:12:57 This blog is copied from: https://medium.com/@jon ...

  6. linux下查看指定进程的所有连接信息(转)

    定位某个进程的网络故障时经常需要用到的一个功能就是查找所有连接的信息.通常查找某个端口的连接信息使用 ss 或者 netstat 可以轻松拿到,如果是主动与别的机器建立的连接信息则可以通过 lsof ...

  7. Jenkins自动化版本构建

    1.拉取代码 2.更新父版本 更新依赖版本 3.打包并推送到maven私库 4.版本控制后提交代码并打成docker镜像 PS:修改pom.xml项目版本,这里我没使用插件,直接使用脚本进行修改,这样 ...

  8. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  9. Flutter常用库:

    flutter_screenutil: ^0.6.0 #用于屏幕适配的包 dio: ^3.0.3 #建立请求需要的包 event_bus: ^1.1.0 #事件发布的包 shared_preferen ...

  10. 【439】Tweets processing by Python

        参数说明: coordinates:Represents the geographic location of this Tweet as reported by the user or cl ...