⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 本代码以J2SE 5.0为开发环境
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			}
			// 获得被选择的文件名
			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);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -