JAVA通过递归遍历FTP中的文件以树形结构显示(JTree)

时间:2024-04-01 12:30:43

1、下载相应的jar包

     commons-net-3.6.jar

2、具体代码如下

package org.ftp.conntion;


import java.io.File;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class ConnectionFtp extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static JTree tree;
	static DefaultTreeModel newModel;
	static DefaultMutableTreeNode Node;
	static DefaultMutableTreeNode temp;
	public ConnectionFtp() throws IOException {
		// TODO Auto-generated constructor stub
		FTPClient ftp = ConnFTP("192.168.1.31", "ao", "tech", 21);	
		Node = ListFTPFile(ftp, ftp.printWorkingDirectory());
		ftp.disconnect();
		newModel = new DefaultTreeModel(Node);
		tree=new JTree(newModel);
		
		this.setSize(400,500);
		this.add(new JScrollPane(tree));
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}
	/*
	 * FTP链接
	 * **/
	public FTPClient ConnFTP(String host,String user,String pass,int port) throws IOException{
		FTPClient ftp=new FTPClient();
		ftp.setControlEncoding("GBK");
		ftp.connect(host, port);
		ftp.login(user, pass);
		return ftp;
		
	}
	
	/*
	 * 关闭ftp链接
	 * */
	public void disConnect(FTPClient ftp){
		try {
			ftp.logout();
			if (ftp.isConnected()) {
				ftp.disconnect();
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	
	public DefaultMutableTreeNode ListFTPFile(FTPClient ftpClient,String path) throws IOException{
		String gbkname;
		InputStream iStream = null;
		DefaultMutableTreeNode fujiedian = new DefaultMutableTreeNode(path);
		System.out.println("当前节点名字:" + fujiedian);
		try {
			FTPFile[] files = ftpClient.listFiles();  //获取当前路径中的文件并存放到files数组中
			System.out.println(ftpClient.printWorkingDirectory() + ":"+files.length);
			for (FTPFile file2:files) {
				if (file2.isDirectory()) {
					String tempDir=ftpClient.printWorkingDirectory()+"/"+file2.getName();
					System.out.println("tempdir:" + tempDir);
					ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
					gbkname=file2.getName();
					ftpClient.storeFile(new String(gbkname.getBytes("GBK"),"iso-8859-1"), iStream);
					System.out.println(ftpClient.printWorkingDirectory());
					ftpClient.changeWorkingDirectory(tempDir);
					ListFTPFile(ftpClient, tempDir);
					fujiedian.add(ListFTPFile(ftpClient, file2.getName()));
					ftpClient.changeToParentDirectory();

				}
				else{
					System.out.println(ftpClient.printWorkingDirectory() + "/" + file2.getName());
					temp = new DefaultMutableTreeNode(file2.getName());
					fujiedian.add(temp);
				}
				
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			System.exit(1);
		}
		return fujiedian;
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new ConnectionFtp();
		
	}
}

运行程序后结果如下 

JAVA通过递归遍历FTP中的文件以树形结构显示(JTree)