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

📄 frmftp.java

📁 用java的ftpClient包和swing编写的带界面的ftp客户端
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * FrmFtp.java
 *
 * Created on __DATE__, __TIME__
 */

package djr.gui;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPException;

import djr.ftp.FtpDownload;
import djr.ftp.FtpUpload;

/**
 *
 * @author  __USER__
 */
public class FrmFtp extends javax.swing.JFrame {

	private String localPath;

	private String remotePath;

	private FTPClient ftpClient;

	/** Creates new form FrmFtp */
	public FrmFtp() {
		initComponents();
	}

	public FrmFtp(FTPClient ftpClient) {
		this.ftpClient = ftpClient;
		initComponents();
		this.customInit();
	}

	public FTPClient getFtpClient() {
		return ftpClient;
	}

	public void setFtpClient(FTPClient ftpClient) {
		this.ftpClient = ftpClient;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
	private void initComponents() {
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jPanel2 = new javax.swing.JPanel();
		uploadBtn = new javax.swing.JButton();
		localCombo = new javax.swing.JComboBox();
		localLabel = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		localList = new javax.swing.JList();
		jPanel3 = new javax.swing.JPanel();
		downloadBtn = new javax.swing.JButton();
		remoteCombo = new javax.swing.JComboBox();
		remoteLabel = new javax.swing.JLabel();
		jScrollPane2 = new javax.swing.JScrollPane();
		remoteList = new javax.swing.JList();
		createDirButton = new javax.swing.JButton();
		renameButton = new javax.swing.JButton();
		deleteButton = new javax.swing.JButton();
		prevBtn = new javax.swing.JButton();
		rootBtn = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jLabel1.setText("DJR FTP \u7cfb\u7edf");

		org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				jPanel1Layout.createSequentialGroup().add(317, 317, 317).add(
						jLabel1).addContainerGap(362, Short.MAX_VALUE)));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				jPanel1Layout.createSequentialGroup().add(jLabel1)
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		jPanel2.setBorder(javax.swing.BorderFactory
				.createTitledBorder("\u672c\u5730\u7cfb\u7edf"));
		jPanel2.setName("1");
		uploadBtn.setText("\u4e0a\u4f20");
		uploadBtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				uploadBtnActionPerformed(evt);
			}
		});

		localCombo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				localComboActionPerformed(evt);
			}
		});

		localLabel.setText("jLabel1");
		localLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

		localList.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				localListMouseClicked(evt);
			}
		});

		jScrollPane1.setViewportView(localList);

		org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(localCombo, 0,
				317, Short.MAX_VALUE).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				jPanel2Layout.createSequentialGroup().add(localLabel,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 255,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED).add(
								uploadBtn)).add(jScrollPane1,
				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 317,
				Short.MAX_VALUE));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								org.jdesktop.layout.GroupLayout.TRAILING,
								jPanel2Layout
										.createSequentialGroup()
										.add(
												localCombo,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jScrollPane1,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												379, Short.MAX_VALUE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jPanel2Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(uploadBtn)
														.add(
																localLabel,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																21,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))));

		jPanel3.setBorder(javax.swing.BorderFactory
				.createTitledBorder("\u8fdc\u7a0b\u7cfb\u7edf"));
		downloadBtn.setText("\u4e0b\u8f7d");
		downloadBtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				downloadBtnActionPerformed(evt);
			}
		});

		remoteCombo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				remoteComboActionPerformed(evt);
			}
		});

		remoteLabel.setText("jLabel2");
		remoteLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

		remoteList.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				remoteListMouseClicked(evt);
			}
		});

		jScrollPane2.setViewportView(remoteList);

		createDirButton.setText("\u65b0\u5efa\u6587\u4ef6\u5939");
		createDirButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				createDirButtonActionPerformed(evt);
			}
		});

		renameButton.setText("\u91cd\u547d\u540d");
		renameButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				renameButtonActionPerformed(evt);
			}
		});

		deleteButton.setText("\u5220\u9664");
		deleteButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				deleteButtonActionPerformed(evt);
			}
		});

		prevBtn.setText("\u4e0a\u4e00\u7ea7");
		prevBtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				prevBtnActionPerformed(evt);
			}
		});

		rootBtn.setText("\u6839\u76ee\u5f55");
		rootBtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				rootBtnActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(
				jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(remoteCombo, 0,
				403, Short.MAX_VALUE).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				jPanel3Layout.createSequentialGroup().add(remoteLabel,
						org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 341,
						Short.MAX_VALUE).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						downloadBtn)).add(
				org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2,
				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 403,
				Short.MAX_VALUE).add(
				jPanel3Layout.createSequentialGroup().add(createDirButton)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED).add(
								renameButton).addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED).add(
								deleteButton).addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED).add(
								prevBtn).addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED).add(
								rootBtn).addContainerGap()));
		jPanel3Layout
				.setVerticalGroup(jPanel3Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								org.jdesktop.layout.GroupLayout.TRAILING,
								jPanel3Layout
										.createSequentialGroup()
										.add(
												remoteCombo,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jScrollPane2,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												351,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												7, Short.MAX_VALUE)
										.add(
												jPanel3Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(createDirButton)
														.add(renameButton).add(
																deleteButton)
														.add(prevBtn).add(
																rootBtn))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jPanel3Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(downloadBtn).add(
																remoteLabel))));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																org.jdesktop.layout.GroupLayout.TRAILING,
																layout
																		.createSequentialGroup()
																		.add(
																				jPanel2,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				jPanel3,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
														.add(
																jPanel1,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jPanel1,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING)
														.add(
																jPanel2,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.add(
																jPanel3,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap()));
		pack();
	}// </editor-fold>//GEN-END:initComponents

	//GEN-FIRST:event_rootBtnActionPerformed
	private void rootBtnActionPerformed(java.awt.event.ActionEvent evt) {
		int lastIndex = this.remotePath.lastIndexOf("\\");
		while (lastIndex > -1) {
			try
			{
				this.ftpClient.cdup();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			catch (FTPException e)
			{
				e.printStackTrace();
			}
			this.remotePath = this.remotePath.substring(0, lastIndex);
			lastIndex = this.remotePath.lastIndexOf("\\");
		}
		this.remoteList.setModel(this.getRemoteListModel("."));
		this.remoteLabel.setText(this.remotePath);
	}//GEN-LAST:event_rootBtnActionPerformed

	//GEN-FIRST:event_prevBtnActionPerformed
	private void prevBtnActionPerformed(java.awt.event.ActionEvent evt) {
		int lastIndex = this.remotePath.lastIndexOf("\\");
		this.remotePath = this.remotePath.substring(0, lastIndex);
		try
		{
			this.ftpClient.cdup();
			this.remoteList.setModel(this.getRemoteListModel(ftpClient
					.pwd()));
			this.remoteLabel.setText(this.remotePath);
			this.downloadBtn.setEnabled(false);
			this.deleteButton.setEnabled(false);
			this.renameButton.setEnabled(false);
		}
		catch (IOException e)

⌨️ 快捷键说明

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