1. 程式人生 > >JAVA通過遞迴遍歷FTP中的檔案以樹形結構顯示(JTree)

JAVA通過遞迴遍歷FTP中的檔案以樹形結構顯示(JTree)

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();
		
	}
}