📄 frmftp.java
字号:
{
e.printStackTrace();
}
catch (FTPException e)
{
e.printStackTrace();
}
}//GEN-LAST:event_prevBtnActionPerformed
//GEN-FIRST:event_formWindowClosing
private void formWindowClosing(java.awt.event.WindowEvent evt) {
try {
ftpClient.quit();
this.dispose();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}//GEN-LAST:event_formWindowClosing
//GEN-FIRST:event_deleteButtonActionPerformed
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
String fileName = (String) this.remoteList.getSelectedValue();
try {
if (fileName != null && fileName.indexOf(".") == -1) { // 删除文件夹
this.deleteDir(fileName);
} else if (fileName != null && fileName.indexOf(".") > 0) { // 删除文件
this.deleteFile(fileName);
}
this.remoteList.setModel(this.getRemoteListModel(ftpClient.pwd()));
} catch (Exception e) {
e.printStackTrace();
}
}//GEN-LAST:event_deleteButtonActionPerformed
//GEN-FIRST:event_renameButtonActionPerformed
private void renameButtonActionPerformed(java.awt.event.ActionEvent evt) {
String newName = (String) JOptionPane.showInputDialog(null,
"Input New Name", "Input", JOptionPane.INFORMATION_MESSAGE,
null, null, null);
String oldName = (String) this.remoteList.getSelectedValue();
try {
ftpClient.rename(oldName, newName);
this.remoteList.setModel(this.getRemoteListModel(ftpClient.pwd()));
} catch (IOException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}//GEN-LAST:event_renameButtonActionPerformed
//GEN-FIRST:event_createDirButtonActionPerformed
private void createDirButtonActionPerformed(java.awt.event.ActionEvent evt) {
String inputDir = (String) JOptionPane.showInputDialog(null,
"Input Dir Name", "Input", JOptionPane.INFORMATION_MESSAGE,
null, null, null);
try {// 创建文件夹
ftpClient.mkdir(inputDir);
this.remoteList.setModel(this.getRemoteListModel(ftpClient.pwd()));
} catch (IOException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}//GEN-LAST:event_createDirButtonActionPerformed
//GEN-FIRST:event_downloadBtnActionPerformed
private void downloadBtnActionPerformed(java.awt.event.ActionEvent evt) {
String select = (String) remoteList.getSelectedValue();
FtpDownload ftpDownload = new FtpDownload(ftpClient);
try {
if (select == null || "..".equals(select)) {
return;
} else if (select.indexOf(".") == -1) {// 下载文件夹
ftpDownload.downloadDir(this.localPath, select);
this.localList.setModel(this.getListModel(this.localPath));
//ftpClient.chdir(this.remotePath);
} else { // 下载文件
ftpDownload.downloadFile(this.localPath, select);
//ftpClient.cdup();
this.localList.setModel(this.getListModel(this.localPath));
//ftpClient.chdir(this.remotePath);
}
} catch (IOException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}//GEN-LAST:event_downloadBtnActionPerformed
//GEN-FIRST:event_uploadBtnActionPerformed
private void uploadBtnActionPerformed(java.awt.event.ActionEvent evt) {
String select = (String) localList.getSelectedValue();
FtpUpload ftpUpload = new FtpUpload(ftpClient);
try {
if (select == null || "..".equals(select)) {
return;
} else if (select.indexOf(".") == -1) { // 上传目录
ftpUpload.uploadDir(this.localPath + select);
//ftpClient.cdup();
this.remoteList.setModel(this.getRemoteListModel(ftpClient
.pwd()));
//ftpClient.chdir(this.remotePath);
} else { // 上传文件
ftpUpload.uploadFile(this.localPath + select);
//ftpClient.cdup();
this.remoteList.setModel(this.getRemoteListModel(ftpClient
.pwd()));
//ftpClient.chdir(this.remotePath);
}
} catch (IOException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}//GEN-LAST:event_uploadBtnActionPerformed
//GEN-FIRST:event_remoteListMouseClicked
private void remoteListMouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 1) {
remoteListSingleClick();
} else if (evt.getClickCount() == 2) {
remoteListDoubleClick();
}
}//GEN-LAST:event_remoteListMouseClicked
//GEN-FIRST:event_localListMouseClicked
private void localListMouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 1) {
// 鼠标单击事件
localListSingleClick();
} else if (evt.getClickCount() == 2) {
// 鼠标双击事件
localListDoubleClick();
}
}//GEN-LAST:event_localListMouseClicked
//GEN-FIRST:event_remoteComboActionPerformed
private void remoteComboActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == remoteCombo) {
}
}//GEN-LAST:event_remoteComboActionPerformed
//GEN-FIRST:event_localComboActionPerformed
private void localComboActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == localCombo) {
localLabel.setText((String) localCombo.getSelectedItem());
localList.setModel(this.getListModel((String) localCombo
.getSelectedItem()));
this.uploadBtn.setEnabled(false);
}
}//GEN-LAST:event_localComboActionPerformed
private void localListSingleClick() {
this.uploadBtn.setEnabled(true);
this.localLabel.setText(this.localPath + "\\"
+ (String) this.localList.getSelectedValue());
}
private void remoteListSingleClick() {
this.downloadBtn.setEnabled(true);
this.renameButton.setEnabled(true);
this.deleteButton.setEnabled(true);
this.remoteLabel.setText(this.remotePath + "\\"
+ (String) this.remoteList.getSelectedValue());
}
private void localListDoubleClick() {
String select = (String) localList.getSelectedValue();
if (select == null) { // 为空时直接返回
return;
} else if (select.indexOf("..") != -1) { // 为返回上一级文件夹时
int lastIndex = this.localPath.lastIndexOf("\\");
this.localPath = this.localPath.substring(0, lastIndex);
this.localList.setModel(this.getListModel(this.localPath));
this.localLabel.setText(this.localPath);
this.uploadBtn.setEnabled(false);
} else if (select.indexOf(".") == -1) {// 为文件夹时
this.localPath = this.localPath + "\\"
+ (String) this.localList.getSelectedValue();
this.localList.setModel(this.getListModel(this.localPath));
this.localLabel.setText(this.localPath);
this.uploadBtn.setEnabled(false);
}
}
private void remoteListDoubleClick() {
String select = (String) remoteList.getSelectedValue();
try {
if (select == null) {
return;
} else if (".".equals(select)) {// 返回到根目录
int lastIndex = this.remotePath.lastIndexOf("\\");
while (lastIndex > -1) {
this.ftpClient.cdup();
this.remotePath = this.remotePath.substring(0, lastIndex);
lastIndex = this.remotePath.lastIndexOf("\\");
}
this.remoteList.setModel(this.getRemoteListModel("."));
this.remoteLabel.setText(this.remotePath);
} else if (select.indexOf("..") != -1) { // 返回到上一级目录
int lastIndex = this.remotePath.lastIndexOf("\\");
this.remotePath = this.remotePath.substring(0, lastIndex);
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);
} else if (select.indexOf(".") == -1) { // 进入到文件夹
this.remotePath = this.remotePath + "\\" + select;
this.remoteList.setModel(this.getRemoteListModel(select));
this.ftpClient.chdir(select);
this.remoteLabel.setText(this.remotePath);
this.downloadBtn.setEnabled(false);
this.deleteButton.setEnabled(false);
this.renameButton.setEnabled(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void customInit() {
try {
// 设置本地下拉框的内容
localCombo.setModel(this.getComboModel());
// 设置本地路径
this.localPath = (String) localCombo.getSelectedItem();
// 将本地路径设置到本地标签栏中
localLabel.setText(localPath);
// 设置本地列表框的内容
localList.setModel(this.getListModel(localPath));
// 设置远程下拉框为不可用
remoteCombo.setEnabled(false);
// 设置远程路经
this.remotePath = ".";
// 将远程路径设置到远程标签栏中
remoteLabel.setText(this.remotePath);
// 设置远程列表框的内容
remoteList.setModel(this.getRemoteListModel(this.remotePath));
// 设置部分按钮为不可用
this.uploadBtn.setEnabled(false);
this.downloadBtn.setEnabled(false);
this.renameButton.setEnabled(false);
this.deleteButton.setEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
}
private DefaultComboBoxModel getComboModel() {
DefaultComboBoxModel model = new DefaultComboBoxModel();
// 获得本地盘符列表
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) {
model.addElement(roots[i].getAbsolutePath());
}
return model;
}
private DefaultComboBoxModel getRemoteModel() {
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(".");
try {
String[] dirNames = ftpClient.dir();
for (int i = 0; i < dirNames.length; i++) {
model.addElement(dirNames[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
return model;
}
private DefaultListModel getListModel(String path) {
DefaultListModel model = new DefaultListModel();
if (path == null) {
return null;
}
File file = new File(path);
String[] list = file.list();
if (list == null) {
return null;
}
ArrayList directoryarray = new ArrayList();
ArrayList filearray = new ArrayList();
for (int i = 0; i < list.length; i++) {
File tmpfile = new File(file.getPath() + "\\" + list[i]);
if (tmpfile.isDirectory()) {
// 将文件夹添加到文件夹集合中
directoryarray.add(list[i]);
} else {
// 将文件添加到文件集合中
filearray.add(list[i]);
}
}
ArrayList combin = new ArrayList();
combin.addAll(directoryarray);
combin.addAll(filearray);
model.addElement("..");
for (int i = 0; i < combin.size(); i++) {
model.addElement(combin.get(i));
}
return model;
}
private DefaultListModel getRemoteListModel(String path) {
DefaultListModel model = new DefaultListModel();
try {
String[] files = ftpClient.dir(path, true);
for (int i = 0; i < files.length; i++) {
int lastIndex = files[i].lastIndexOf(":");
files[i] = files[i].substring(lastIndex + 4);
model.addElement(files[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
return model;
}
private void deleteDir(String dir) throws Exception {
String[] subFileNames = ftpClient.dir(dir, true);
ftpClient.chdir(dir);
for (int i = 0; i < subFileNames.length; i++) {
int lastIndex = subFileNames[i].lastIndexOf(":");
subFileNames[i] = subFileNames[i].substring(lastIndex + 4);
if (subFileNames[i] == null || ".".equals(subFileNames[i])
|| "..".equals(subFileNames[i])) {
continue;
} else if (subFileNames[i].indexOf(".") == -1) {
this.deleteDir(subFileNames[i]);
} else {
this.deleteFile(subFileNames[i]);
}
}
ftpClient.cdup();
ftpClient.rmdir(dir);
}
private void deleteFile(String fileName) throws Exception {
ftpClient.delete(fileName);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrmFtp().setVisible(true);
}
});
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton createDirButton;
private javax.swing.JButton deleteButton;
private javax.swing.JButton downloadBtn;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JComboBox localCombo;
private javax.swing.JLabel localLabel;
private javax.swing.JList localList;
private javax.swing.JButton prevBtn;
private javax.swing.JComboBox remoteCombo;
private javax.swing.JLabel remoteLabel;
private javax.swing.JList remoteList;
private javax.swing.JButton renameButton;
private javax.swing.JButton rootBtn;
private javax.swing.JButton uploadBtn;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -