生成ftp文件的目录树
生活随笔
收集整理的這篇文章主要介紹了
生成ftp文件的目录树
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
依賴
<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>節(jié)點對象
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);} }生成節(jié)點目錄樹結(jié)構(gòu)
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 連接的節(jié)點(相對根路徑的文件夾)* @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發(fā)布ftp站點,則需要勾掉“高級選項”中的“對所有已收發(fā)的路徑和文件名使用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 + "拒絕連接");}//登陸ftpboolean login = ftp.login(username, password);if (logger.isDebugEnabled()) {if (login) {logger.debug("登陸FTP成功! ip: " + server);} else {logger.debug("登陸FTP失敗! ip: " + server);}}//根據(jù)輸入的路徑,切換工作目錄。這樣ftp端的路徑就可以使用相對路徑了exchageDir(path, ftp);return ftp;}/*** 切換目錄 返回切換的層級數(shù)* @param path* @param ftp* @return 切換的層級數(shù)* @throws IOException*/private static int exchageDir(String path, FTPClient ftp) {// 切換的次數(shù)(層級),方便回退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;}/*** 遍歷樹結(jié)構(gòu)* @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);}}轉(zhuǎn)載于:https://www.cnblogs.com/qiaozhuangshi/p/11216455.html
總結(jié)
以上是生活随笔為你收集整理的生成ftp文件的目录树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从ftp获取文件并生成压缩包
- 下一篇: 深入理解面向对象 -- 基于 JavaS