FTP客戶端的實現

1.Connector類

/**
 * 連接FTP服務器
 */
public class Connector extends Thread {

	MainFrame frame = null;
	// 需要IP地址、用戶名、密碼
	String ip = "";
	String username = "";
	String password = "";

	public Connector(MainFrame frame, String ip, String username,
			String password) {
		// 初始化信息
		this.frame = frame;
		this.ip = ip;
		this.username = username;
		this.password = password;
	}

	public void run() {
		try {
			frame.consoleTextArea.append("connecting the server " + ip
					+ ",wait for a moment..\n");
			frame.ftpClient = new FtpClient();
			// 連接服務器
			frame.ftpClient.openServer(ip);
			frame.consoleTextArea.append("connect server " + ip
					+ " succeed,please continue!\n");
			// 用用戶名和密碼登陸
			frame.ftpClient.login(username, password);
			frame.currentDirTextField.setText("/");
			frame.setTableData();
			frame.serverIPTextField.setText(ip);
			frame.userNameTextField.setText(username);
			frame.passwordTextField.setText(password);
		} catch (Exception e) {
			frame.consoleTextArea.append("can not connect the server " + ip + "!\n");
		}
	}

}

2.UploadFileThread類

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

import sun.net.ftp.FtpClient;

/**
 * 上傳文件到FTP服務器
 */
public class UploadFileThread extends Thread {

	private boolean running = false;
	// FTP服務器IP地址
	String ip = "";
	// 連接服務器的用戶名和密碼
	String username = "";
	String password = "";
	// 文件上傳到FTP服務器上的目錄和文件名
	String ftpDir = "";
	String ftpFileName = "";
	// 待上傳的文件的文件全名
	String localFileFullName = "";
	
	MainFrame frame = null;

	// 構造方法
	public UploadFileThread(MainFrame frame, String serverIP, String username,
			String password, String ftpDir, String ftpFileName, String localFileName) {
		this.ip = serverIP;
		this.username = username;
		this.password = password;
		this.ftpDir = ftpDir;
		this.ftpFileName = ftpFileName;
		this.localFileFullName = localFileName;
		this.frame = frame;
	}

	public void run() {
		running = true;
		FtpClient ftpClient = null;
		OutputStream os = null;
		FileInputStream is = null;
		try {
			String savefilename = localFileFullName;
			// 新建一個FTP客戶端連接
			ftpClient = new FtpClient();
			ftpClient.openServer(ip);
			// 登陸到FTP服務器
			ftpClient.login(username, password);
			if (ftpDir.length() != 0){
				// 切換到目標目錄下
				ftpClient.cd(ftpDir);
			}
			// 以二進制打開FTP
			ftpClient.binary();
			// 準備在FTP服務器上存放文件
			os = ftpClient.put(ftpFileName);
			// 打開本地待上傳的文件
			File file_in = new File(savefilename);
			is = new FileInputStream(file_in);
			byte[] bytes = new byte[1024];

			// 開始拷貝
			int c;
			frame.taskList.add(ftpFileName);
			frame.consoleTextArea.append("uploading the file " + ftpFileName
					+ " , wait for a moment!\n");
			while (running && ((c = is.read(bytes)) != -1)) {
				os.write(bytes, 0, c);
			}
			if (running) {
				// 此時已經上傳完畢,從任務隊列中刪除本上傳任務
				frame.taskList.remove(ftpFileName);
				// 控制檯信息中添加文件上傳完畢的信息
				frame.consoleTextArea.append(" the file " + ftpFileName
						+ " upload has finished!\n");
				// 更新表格數據
				frame.setTableData();
				// 清除任務線程
				frame.performTaskThreads.removeElement(this);
			}

		} catch (Exception e) {
			System.out.println(e.toString());
			// 上傳失敗
			frame.consoleTextArea.append(" the file " + ftpFileName
					+ " ,upload has problem!\n");
		} finally {
			try {
				if (is != null){
					is.close();
				}
				if (os != null){
					os.close();
				}
				if (ftpClient != null){
					ftpClient.closeServer();
				}
			} catch (Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public void toStop(){
		this.running = false;
	}
}

3.DownloadFileThread類

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import sun.net.ftp.FtpClient;

/**
 * 下載文件的線程
 */
public class DownloadFileThread extends Thread {
	
	private boolean running = false;
	
	// FTP服務器的IP
	String ip = "";
	// 連接服務器的用戶名和密碼
	String username = "";
	String password = "";
	// 待下載文件在FTP上的目錄和文件名
	String ftpDir = "";
	String ftpFileName = "";
	// 下載到本地後的文件名
	String localFileName = "";
	
	MainFrame frame = null;

	// 構造方法
	public DownloadFileThread(MainFrame frame, String server, String username,
			String password, String path, String filename, String userpath) {
		this.ip = server;
		this.username = username;
		this.password = password;
		this.ftpDir = path;
		this.ftpFileName = filename;
		this.localFileName = userpath;
		this.frame = frame;
	}

	public void run() {
		running = true;
		FileOutputStream os = null;
		InputStream is = null;
		FtpClient ftpClient = null;
		try {
			String savefilename = localFileName;
			// 新建FTP客戶端
			ftpClient = new FtpClient();
			ftpClient.openServer(ip);
			// 登陸
			ftpClient.login(username, password);
			if (ftpDir.length() != 0){
				// 切換到指定的目錄下
				ftpClient.cd(ftpDir);
			}
			// 以二進制格式打開FTP
			ftpClient.binary();
			// 打開文件
			is = ftpClient.get(ftpFileName);
			// 保存到本地
			File file_out = new File(savefilename);
			os = new FileOutputStream(file_out);
			byte[] bytes = new byte[1024];
			// 開始複製
			int c;
			frame.taskList.add(ftpFileName);
			frame.consoleTextArea.append("downloading the file " + ftpFileName
					+ " , wait for a moment!\n");
			while (running && ((c = is.read(bytes)) != -1)) {
				os.write(bytes, 0, c);
			}
			if (running){
				// 下載成功後,清除任務和線程
				frame.taskList.remove(ftpFileName);
				frame.consoleTextArea.append(" the file " + ftpFileName
						+ " download has finished!\n");
				frame.performTaskThreads.removeElement(this);
			}

		} catch (Exception e) {
			// 下載失敗
			frame.consoleTextArea.append(" the file " + ftpFileName
					+ " ,download has problem!\n");
			frame.performTaskThreads.removeElement(this);
		} finally {
			try {
				if (is != null){
					is.close();
				}
				if (os != null){
					os.close();
				}
				if (ftpClient != null){
					ftpClient.closeServer();
				}
			} catch (Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public void toStop(){
		this.running = false;
	}

}

4.MainFrame類

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.io.InputStream;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import book.string.ChangeCharset;

import sun.net.ftp.FtpClient;

public class MainFrame extends JFrame {
	//連接服務器的按鈕
	JButton connectButton = new JButton("連 接");
	// FTP服務器IP地址輸入框
	JLabel IPLabel = new JLabel("IP:");
	JTextField serverIPTextField = new JTextField(16);
	// /用戶名標籤和輸入框
	JLabel userNameLabel = new JLabel("用戶名:");
	JTextField userNameTextField = new JTextField(16);
	// 密碼標籤和輸入框
	JLabel passwordLabel = new JLabel("密碼:");
	JTextField passwordTextField = new JTextField(16);
	// 當前目錄標籤和輸入框
	JLabel currentDirLabel = new JLabel("當前目錄:");
	JTextField currentDirTextField = new JTextField(30);
	// 轉入上一級目錄的按鈕
	JButton upLevelButton = new JButton("上一級目錄");
	// 上載和下載文件的按鈕
	JButton downloadButton = new JButton("下載");
	JButton uploadButton = new JButton("上傳");
	// 斷開和退出的按鈕
	JButton disconnectButton = new JButton("斷開");
	JButton exitButton = new JButton("關閉");

	// 表格的數據,用於顯示FTP服務器上的信息
	String[] columnname = { "文件名", "文件大小", "修改日期" };
	DefaultTableModel tableModel = new DefaultTableModel();
	JTable ftpFileInfosTable = new JTable();
	JScrollPane ftpFileScrollPane1 = new JScrollPane(ftpFileInfosTable);

	// 下載和上傳的文件對話框
	FileDialog saveFileDialog = new FileDialog(this, "Download To ..",
			FileDialog.SAVE);
	FileDialog openFileDialog = new FileDialog(this, "Upload File ..",
			FileDialog.LOAD);

	FtpClient ftpClient = null;
	// IP地址、用戶名、密碼、FTP路徑
	String ip = "";
	String username = "";
	String password = "";
	String path = "";

	// 當前FTP目錄下的文件和目錄信息
	Vector files = new Vector();
	Vector fileSizes = new Vector();
	Vector fileTypes = new Vector();
	Vector fileDates = new Vector();

	// 當前選擇的表格中的行號和列號
	int row = 0;
	int column = 0;

	// 當前正在執行上下載任務的線程
	Vector performTaskThreads = new Vector();
	// 顯示當前的任務列表
	java.awt.List taskList = new List();
	// 顯示FTP客戶端程序的控制檯信息
	JTextArea consoleTextArea = new JTextArea();

	// 因爲從FTP中取得編碼是ISO-8859-1,爲了支持中文,所以將其轉換。
	// 這個類是在將字符串的章節中定義的,這裏直接使用了。
	ChangeCharset changeCharset = new ChangeCharset();
	
	public MainFrame() {
		init();
		setLocation(100, 150);
		setTitle("FTP 客戶端");
		pack();
		try {
			// 設置界面爲系統默認外觀
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void init(){
		// 初始化各個文本框
		serverIPTextField.setText("127.0.0.1");
		userNameTextField.setText("");
		passwordTextField.setText("");
		currentDirTextField.setText("");
		currentDirTextField.setEditable(false);

		// 將IP、用戶名、密碼、連接標籤和輸入框放在面板panel1中
		JPanel panel1 = new JPanel();
		panel1.setLayout(new FlowLayout());
		panel1.add(IPLabel);
		panel1.add(serverIPTextField);
		panel1.add(userNameLabel);
		panel1.add(userNameTextField);
		panel1.add(passwordLabel);
		panel1.add(passwordTextField);
		panel1.add(connectButton);

		// 將當前目錄、上一級目錄的標籤、輸入框和按鈕放在面板panel2中
		JPanel panel2 = new JPanel();
		panel2.setLayout(new FlowLayout());
		panel2.add(currentDirLabel);
		panel2.add(currentDirTextField);
		panel2.add(upLevelButton);
		
		JPanel panelA = new JPanel();
		panelA.setLayout(new BorderLayout());
		panelA.add(panel1, BorderLayout.NORTH);
		panelA.add(panel2, BorderLayout.CENTER);

		ftpFileScrollPane1.setBorder(new TitledBorder(BorderFactory
				.createEtchedBorder(Color.white, new Color(134, 134, 134)),
				"FTP服務器上文件信息"));

		// 將下載、上傳、斷開、退出按鈕放在panel3中
		JPanel panel3 = new JPanel();
		panel3.setLayout(new FlowLayout());
		panel3.add(downloadButton);
		panel3.add(uploadButton);
		panel3.add(disconnectButton);
		panel3.add(exitButton);

		// 將任務列表放在有滾動條的面板中
		JScrollPane taskScrollPane = new JScrollPane(taskList);
		taskScrollPane.setBorder(new TitledBorder(BorderFactory
				.createEtchedBorder(Color.white, new Color(134, 134, 134)), "上下載隊列"));
		
		// 將控制檯信息的文本域放在滾動條面板中,並且設置爲不可編輯
		consoleTextArea.setBackground(SystemColor.control);
		consoleTextArea.setEditable(false);
		consoleTextArea.setText("");
		consoleTextArea.setRows(5);
		JScrollPane consoleScrollPane = new JScrollPane(consoleTextArea);
		consoleScrollPane.setBorder(new TitledBorder(BorderFactory
				.createEtchedBorder(Color.white, new Color(134, 134, 134)), "上下載信息"));
		
		JPanel panelB = new JPanel();
		panelB.setLayout(new BorderLayout());
		panelB.add(panel3, BorderLayout.NORTH);
		panelB.add(taskScrollPane, BorderLayout.CENTER);
		panelB.add(consoleScrollPane, BorderLayout.SOUTH);
		
		// 將各個面板添加到主面板中
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(panelA, BorderLayout.NORTH);
		this.getContentPane().add(ftpFileScrollPane1, BorderLayout.CENTER);
		this.getContentPane().add(panelB, BorderLayout.SOUTH);
		
		// 爲按鈕添加事件處理器
		connectButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				connectButton_actionPerformed(e);
			}
		});
		upLevelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				upLevelButton_actionPerformed(e);
			}
		});
		downloadButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				downloadButton_actionPerformed(e);
			}
		});
		uploadButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				uploadButton_actionPerformed(e);
			}
		});
		disconnectButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				disconnectButton_actionPerformed(e);
			}
		});
		exitButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				exitButton_actionPerformed(e);
			}
		});
		ftpFileInfosTable.addMouseListener(new MainFrame_Table_mouseAdapter(
				this));

	}

	/**
	 * 處理窗口關閉事件
	 */
	protected void processWindowEvent(WindowEvent e) {
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			disconnection();
			System.exit(0);
		}
	}
	/**
	 * 處理連接按鈕發生的事件
	 * @param e
	 */
	void connectButton_actionPerformed(ActionEvent e) {
		// 獲得IP、用戶名和密碼
		ip = serverIPTextField.getText();
		username = userNameTextField.getText();
		if (username.equals("")){
			username = "anonymous";
		}
		password = passwordTextField.getText();
		if (password.equals("")){
			password = "anonymous";
		}
		if (ip.equals(""))
			return;

		try {
			// 連接服務器
			Connector conn = new Connector(this, ip, username, password);
			conn.start();
		} catch (Exception ee) {
		}
	}

	/**
	 * 轉入上一級目錄按鈕事件處理
	 * @param e
	 */
	void upLevelButton_actionPerformed(ActionEvent e) {
		upDirectory();
	}

	/**
	 * 下載按鈕事件處理
	 * @param e
	 */
	void downloadButton_actionPerformed(ActionEvent e) {
		try {
			// 獲得被表格中被選擇的文件名
			String str = (String) files.elementAt(row);
			// 如果被選擇的是一個文件,則下載。
			String t = (String) fileTypes.elementAt(row);
			if (t.startsWith("-")){
				// 表明是個文件類型
				download(str, "");
			}
		} catch (Exception ee) {
			consoleTextArea.append("file download has problems!\n");
		}

	}
	/**
	 * 上傳按鈕事件處理
	 * @param e
	 */
	void uploadButton_actionPerformed(ActionEvent e) {
		upload();

	}
	/**
	 * 斷開連接按鈕事件處理
	 * @param e
	 */
	void disconnectButton_actionPerformed(ActionEvent e) {
		disconnection();
	}

	/**
	 * 退出FTP客戶端按鈕事件處理
	 * @param e
	 */
	void exitButton_actionPerformed(ActionEvent e) {
		System.exit(0);
	}

	/**
	 * 上傳文件到FTP
	 */
	void upload() {
		try {
			// 顯示上傳文件的對話框
			openFileDialog.show();
			String directory = openFileDialog.getDirectory();
			if (directory == null || directory.equals("")){
				return;
			}
			// 獲得被選擇的文件名
			String file = openFileDialog.getFile();
			if (file == null || file.equals("")){
				return;
			}
			String filename = directory + file;
			// 新建一個上傳文件的任務線程,並啓動
			UploadFileThread up = new UploadFileThread(this, ip, username,
					password, path, file, filename);
			up.start();
			// 保存到任務線程組中
			performTaskThreads.addElement(up);

		} catch (Exception ee) {
			consoleTextArea.append("file upload has problems!\n ");
			return;
		}
	}

	/**
	 * 下載文件到本地
	 * @param filename
	 * @param directory
	 */
	void download(String filename, String directory) {
		try {
			// 獲得存放文件的本地目錄和文件名
			if (directory.equals("")) {
				saveFileDialog.setFile(filename);
				saveFileDialog.show();
				// 本地目錄和文件名
				directory = saveFileDialog.getDirectory();
				String file = saveFileDialog.getFile();
				if (directory == null || directory.equals("")){
					return;
				}
				if (file == null || file.equals("")){
					file = filename;
				} else {
					// 先獲得帶下載的文件名的後綴
					int index = filename.lastIndexOf(".");
					String extn = filename.substring(index + 1);
					// 如果本地文件名和待下載文件名的後綴不一樣,
					// 則將本地文件名後面再追加待下載文件名的後綴
					index = file.lastIndexOf(".");
					String extn1 = file.substring(index + 1);
					if (!extn.equals(extn1)){
						file = file + "." + extn;
					}
				}
				directory = directory + file;
			} else {
				directory += filename;
			}
			// 啓動一個下載文件的線程,並啓動
			DownloadFileThread down = new DownloadFileThread(this, ip,
					username, password, path, filename, directory);
			down.start();
			performTaskThreads.add(down);
		} catch (Exception ee) {
			consoleTextArea.append("file" + filename + "has problems!");
		}
	}
	/**
	 * 斷開FTP客戶端連接
	 */	
	public void disconnection() {
		try {
			// 清除所有的任務線程
			if (performTaskThreads.size() != 0) {
				if ((JOptionPane.showConfirmDialog(this, "還有任務沒有執行完,確定退出?",
						"斷開連接", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)) {
					return;
				}
				try {
					for (int i = 0; i < performTaskThreads.size(); i++) {
						Object thread = performTaskThreads.elementAt(i);
						if (thread instanceof DownloadFileThread){
							DownloadFileThread down = (DownloadFileThread)thread;
							down.toStop();
						} else if (thread instanceof UploadFileThread){
							UploadFileThread upload = (UploadFileThread)thread;
							upload.toStop();
						}

					}
				} catch (Exception ee) {
					System.out.println(ee.toString());
				}
				taskList.removeAll();
			}
			//清除保存的一些數據
			files.removeAllElements();
			fileTypes.removeAllElements();
			fileSizes.removeAllElements();
			fileDates.removeAllElements();

			serverIPTextField.setText("");
			userNameTextField.setText("");
			passwordTextField.setText("");
			currentDirTextField.setText("");
			consoleTextArea.append("server " + ip + " disconnected!\n");

			ip = "";
			username = "";
			password = "";
			path = "";
			
			// 清除表格
			String[][] disdata = null;
			String[] col = null;
			tableModel.setDataVector(disdata, col);
			ftpFileInfosTable.setModel(tableModel);

			// 關閉FTP客戶端
			ftpClient.closeServer();
		} catch (Exception ee) {
			consoleTextArea.append("server " + ip
					+ " dicconect has problems!\n");
		}
	}

	// 設置表格數據
	void setTableData() {
		try {
			// 首先清除文件列表信息
			if (files.size() != 0) {
				files.removeAllElements();
				fileTypes.removeAllElements();
				fileSizes.removeAllElements();
				fileDates.removeAllElements();
			}
			String list = "";
			// 切換到當前目錄
			ftpClient.cd("/");
			if (!path.equals("")){
				ftpClient.cd(path);
			}
			// List當前目錄下的數據、包括目錄和文件名
			InputStream is = ftpClient.list();
			int c;
			while ((c = is.read()) != -1) {
				String s = (new Character((char) c)).toString();
				list += s;
			}
			is.close();
			if (!list.equals("")) {
				// 解析List命令得到的數據,得到當前目錄下的目錄、文件名、大小、類型
				StringTokenizer st = new StringTokenizer(list, "\n");
				int count = st.countTokens();
				for (int i = 0; i < count; i++) {
					String s = st.nextToken();
					StringTokenizer sst = new StringTokenizer(s, " ");
					c = sst.countTokens();

					String datastr = "";
					String filestr = "";

					for (int j = 0; j < c; j++) {
						String ss = sst.nextToken();
						if (j == 0){
							fileTypes.addElement(
									changeCharset.changeCharset(
											ss,ChangeCharset.ISO_8859_1, ChangeCharset.GBK));
						} else if (j == 4) {
							System.out.println(ss);
							fileSizes.addElement(ss);
						} else if (j == 5) {
							datastr = ss;
						} else if (j == 6) {
							datastr += " " + ss;
						} else if (j == 7) {
							datastr += " " + ss;
							fileDates.addElement(
									changeCharset.changeCharset(
											datastr,ChangeCharset.ISO_8859_1, ChangeCharset.GBK));
						} else if (j == 8) {
							if (c == 9){
								files.addElement(
										changeCharset.changeCharset(
												ss,ChangeCharset.ISO_8859_1, ChangeCharset.GBK));
							} else {
								filestr = ss;
							}
						} else if (j > 8) {
							filestr += " " + ss;
							if (j == (c - 1)){
								files.addElement(
										changeCharset.changeCharset(
												filestr,ChangeCharset.ISO_8859_1, ChangeCharset.GBK));
							}
						}
					}
				}
				int cc = files.size();
				String[][] mydata = new String[cc][3];

				for (int i = 0; i < cc; i++) {
					mydata[i][0] = (String) files.elementAt(i);
					mydata[i][1] = (String) fileSizes.elementAt(i);
					mydata[i][2] = (String) fileDates.elementAt(i);
				}
				// 更新表格
				tableModel.setDataVector(mydata, columnname);
				ftpFileInfosTable.setModel(tableModel);
			}
		} catch (Exception ee) {
			consoleTextArea.append("Read directory has problem !\n");
			return;
		}
	}

	/**
	 * 轉入上一級目錄
	 */
	void upDirectory() {
		// 如果已經到了最頂級目錄,則不處理。
		if (path.equals("/")){
			return;
		}
		try {
			// 將目錄上以及處理。
			StringTokenizer st = new StringTokenizer(path, "/");
			int count = st.countTokens();

			String s = "";
			for (int i = 0; i < count - 1; i++){
				s += st.nextToken() + "/";
			}
			if (s.length() != 0){
				path = s.substring(0, s.length() - 1);
			} else {
				path = "";
			}
			currentDirTextField.setText(path);
			setTableData();
		} catch (Exception ee) {
			consoleTextArea.append("go to parent directory has problems!");
		}
	}

	/**
	 * 選擇表格中的數據是的事件處理
	 * @param e
	 */
	void table_mousePressed(MouseEvent e) {
		// 只處理鼠標左鍵的事件
		if (SwingUtilities.isLeftMouseButton(e)) {
			// 獲得鼠標的位置
			Point point = e.getPoint();
			// 得到選擇的行和列的值
			row = ftpFileInfosTable.rowAtPoint(point);
			column = ftpFileInfosTable.columnAtPoint(point);

			try {
				// 判斷鼠標點擊的次數
				int count = e.getClickCount();
				// 如果選擇的是第0列,則表示選擇的是文件或者目錄名
				if (column == 0) {
					// 獲得該位置上的文件名
					String str = (String) files.elementAt(row);
					// 雙擊
					if (count == 2) {
						// 如果選擇的文件名是".",則爲轉入根目錄命令
						if (str.equals(".")) {
							path = "/";
							setTableData();
							return;
						} else if (str.equals("..")) {
							// 如果選擇的文件名是"..",則轉入上以及目錄
							upDirectory();
							return;
						}
						// 如果選擇是目錄,則進入該目錄
						String s = (String) fileTypes.elementAt(row);
						if (s.startsWith("d")) {
							if (path.equals("")){
								path = "/" + str;
							} else {
								path += "/" + str;
							}
							currentDirTextField.setText(path);
							setTableData();
						} else if (s.startsWith("-")) {
							// 如果選擇的是文件,則下載
							download(str, "");
						}
					}
				}
			} catch (Exception ee) {
				consoleTextArea.append("download or read file has problems!\n");
			}

		}
	}
	
	public static void main(String[] args) {
		MainFrame frame = new MainFrame();
		frame.setVisible(true);
	}
}

class MainFrame_Table_mouseAdapter extends java.awt.event.MouseAdapter {
	MainFrame adaptee;

	MainFrame_Table_mouseAdapter(MainFrame adaptee) {
		this.adaptee = adaptee;
	}

	public void mousePressed(MouseEvent e) {
		adaptee.table_mousePressed(e);
	}

}


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章