Home > Java > javaTutorial > body text

Implementing FTP local file management module function example using java (ftp software development three)

Y2J
Release: 2017-04-21 16:53:32
Original
2195 people have browsed it

This article shares the implementation method of the FTP local file management module for your reference. The specific content is as follows

First take a look at the interface:

Implementing FTP local file management module function example using java (ftp software development three)

1. Display function of local file list

Displays all files in the current local directory, and displays the file attributes including file name, size, date, and javax. swing.JTable() to display specific data. Changing the current file directory will call the listLocalFiles() method of the com.oyp.ftp.panel.local.LocalPanel class. Its main code is as follows


 /** 
 * 读取本地文件到表格的方法 
 */ 
private void listLocalFiles(File selDisk) { 
 if (selDisk == null || selDisk.isFile()) { 
 return; 
 } 
 localSelFilePathLabel.setText(selDisk.getAbsolutePath()); 
 File[] listFiles = selDisk.listFiles(); // 获取磁盘文件列表 
 // 获取表格的数据模型 
 DefaultTableModel model = (DefaultTableModel) localDiskTable.getModel(); 
 model.setRowCount(0); // 清除模型的内容 
 model.addRow(new Object[] { ".", "<DIR>", "" }); // 创建.选项 
 model.addRow(new Object[] { "..", "<DIR>", "" }); // 创建..选项 
 if (listFiles == null) { 
 JOptionPane.showMessageDialog(this, "该磁盘无法访问"); 
 return; 
 } 
 // 遍历磁盘根文件夹的内容,添加到表格中 
 for (File file : listFiles) { 
 File diskFile = new DiskFile(file); // 创建文件对象 
 String length = file.length() + "B "; // 获取文件大小 
 if (file.length() > 1000 * 1000 * 1000) { // 计算文件G单位 
 length = file.length() / 1000000000 + "G "; 
 } 
 if (file.length() > 1000 * 1000) { // 计算文件M单位 
 length = file.length() / 1000000 + "M "; 
 } 
 if (file.length() > 1000) { 
 length = file.length() / 1000 + "K "; // 计算文件K单位 
 } 
 if (file.isDirectory()) { // 显示文件夹标志 
 length = "<DIR>"; 
 } 
 // 获取文件的最后修改日期 
 String modifDate = new Date(file.lastModified()).toLocaleString(); 
 if (!file.canRead()) { 
 length = "未知"; 
 modifDate = "未知"; 
 } 
 // 将单个文件的信息添加到表格的数据模型中 
 model.addRow(new Object[] { diskFile, length, modifDate }); 
 } 
 localDiskTable.clearSelection(); // 取消表格的选择项 
}
Copy after login

2. Local Refresh function of file list

Clicking the "Refresh" button will trigger the actionPerformed(ActionEvent e) method of the com.oyp.ftp.panel.local.RefreshAction class. Its main code is as follows


/** 
 * 刷新本地资源列表的动作处理器的事件处理方法 
 */ 
 @Override 
 public void actionPerformed(ActionEvent e) { 
 this.localPanel.refreshCurrentFolder(); // 调用管理面板的刷新方法 
 }
Copy after login

The above response event will call the refreshCurrentFolder() method of the com.oyp.ftp.panel.local.LocalPanel class. The specific code is as follows


 /** 
 * 刷新指定文件夹的方法 
 */ 
void refreshFolder(File file) { 
 listLocalFiles(file); 
} 
 
/** 
 * 刷新本地当前文件夹的方法 
 */ 
public void refreshCurrentFolder() { 
 final File file = getCurrentFolder(); // 获取当前文件夹 
 Runnable runnable = new Runnable() { // 创建新的线程 
 public void run() { 
 listLocalFiles(file); // 重载当前文件夹的列表到表格中 
 } 
 }; 
 //导致 runnable 的 run 方法在 EventQueue 的指派线程上被调用。 
 SwingUtilities.invokeLater(runnable); // 在事件线程中调用该线程对象 
}
Copy after login

3. New local folder function

Clicking the "New Folder" button will trigger the actionPerformed(() of the com.oyp.ftp.panel.local.CreateFolderAction class ActionEvent e) method, and then a dialog box pops up, fill in the name of the folder to be created, select "OK", and click the "Cancel" button to end. The main code is as follows


/** 
 * 创建文件夹按钮的动作处理器的动作事件的方法 
 */ 
 @Override 
 public void actionPerformed(ActionEvent e) { 
 // 使用输入对话框接收用户输入的文件夹名称 
 String folderName = JOptionPane.showInputDialog("请输入文件夹名称:"); 
 if (folderName == null) 
 return; 
 File curFolder = null; 
 // 获取本地资源表格的当前选择行号 
 int selRow = localPanel.localDiskTable.getSelectedRow(); 
 if (selRow < 0) { 
 // 创建当前文件夹对象 
 curFolder = new File(localPanel.localSelFilePathLabel.getText()); 
 } else { 
 // 获取表格选择行的第一个单元值 
 Object value = localPanel.localDiskTable.getValueAt(selRow, 0); 
 if (value instanceof File) { // 如果单元值是文件,则获取文件的上级文件夹 
 curFolder = (File) value; 
 if (curFolder.getParentFile() != null) 
  curFolder = curFolder.getParentFile(); 
 } else 
 // 否则根据界面的路径标签创建当前文件夹对象 
 curFolder = new File(localPanel.localSelFilePathLabel.getText()); 
 } 
 // 创建当前文件夹下的新文件夹对象 
 File tempFile = new File(curFolder, folderName); 
 if (tempFile.exists()) {// 如果存在相同文件或文件夹 
 JOptionPane.showMessageDialog(localPanel, folderName 
  + "创建失败,已经存在此名称的文件夹或文件。", "创建文件夹", 
  JOptionPane.ERROR_MESSAGE);// 提示用户名称已存在 
 return; // 结束本方法 
 } 
 if (tempFile.mkdir()) // 创建文件夹 
 JOptionPane.showMessageDialog(localPanel, folderName + "文件夹,创建成功。", 
  "创建文件夹", JOptionPane.INFORMATION_MESSAGE); 
 else 
 JOptionPane.showMessageDialog(localPanel, folderName + "文件夹无法被创建。", 
  "创建文件夹", JOptionPane.ERROR_MESSAGE); 
 this.localPanel.refreshFolder(curFolder);// 刷新文件夹 
 }
Copy after login

4. Delete local file function

Select the file or folder to be deleted and click " Delete" button will trigger the actionPerformed(ActionEvent e) method of the com.oyp.ftp.panel.local.DelFileAction class, and then a dialog box will pop up, select "Yes", "No", and end with the "Cancel" button. The main code is as follows


/** 
 * 删除本地文件的动作处理器的处理动作事件的方法 
 */ 
 public void actionPerformed(ActionEvent e) { 
 // 获取表格选择的所有行 
 final int[] selRows = this.localPanel.localDiskTable.getSelectedRows(); 
 if (selRows.length < 1) // 如果没有选择表格内容 
 return; // 结束该方法 
 int confirmDialog = JOptionPane.showConfirmDialog(localPanel, 
 "确定要执行删除吗?"); // 用户确认是否删除 
 if (confirmDialog == JOptionPane.YES_OPTION) { // 如果用于同意删除 
 Runnable runnable = new Runnable() { // 创建线程 
 /** 
  * 删除文件的递归方法 
  * 
  * @param file 
  * 要删除的文件对象 
  */ 
 private void delFile(File file) { 
  try { 
  if (file.isFile()) { // 如果删除的是文件 
  boolean delete = file.delete(); // 调用删该文件的方法 
  if (!delete) { 
  JOptionPane.showMessageDialog(localPanel, file 
   .getAbsoluteFile() 
   + "文件无法删除。", "删除文件", 
   JOptionPane.ERROR_MESSAGE); 
  return; 
  } 
  } else if (file.isDirectory()) { // 如果删除的是文件夹 
  File[] listFiles = file.listFiles();// 获取该文件夹的文件列表 
  if (listFiles.length > 0) { 
  for (File subFile : listFiles) { 
   delFile(subFile); // 调用递归方法删除该列表的所有文件或文件夹 
  } 
  } 
  boolean delete = file.delete();// 最后删除该文件夹 
  if (!delete) { // 如果成功删除 
  JOptionPane.showMessageDialog(localPanel, file 
   .getAbsoluteFile() 
   + "文件夹无法删除。", "删除文件", 
   JOptionPane.ERROR_MESSAGE); 
  return; // 返回方法的调用处 
  } 
  } 
  } catch (Exception ex) { 
  Logger.getLogger(LocalPanel.class.getName()).log( 
  Level.SEVERE, null, ex); 
  } 
 } 
 
 /** 
  * 线程的主体方法 
  * 
  * @see java.lang.Runnable#run() 
  */ 
 public void run() { 
  File parent = null; 
  // 遍历表格的选择内容 
  for (int i = 0; i < selRows.length; i++) { 
  // 获取每个选择行的第一列单元内容 
  Object value = DelFileAction.this.localPanel.localDiskTable 
  .getValueAt(selRows[i], 0); 
  // 如果该内容不是DiskFile类的实例对象 
  if (!(value instanceof DiskFile)) 
  continue; // 结束本次循环 
  DiskFile file = (DiskFile) value; 
  if (parent == null) 
  parent = file.getParentFile(); // 获取选择文件的上级文件夹 
  if (file != null) { 
  delFile(file); // 调用递归方法删除选择内容 
  } 
  } 
  // 调用refreshFolder方法刷新当前文件夹 
  DelFileAction.this.localPanel.refreshFolder(parent); 
  JOptionPane.showMessageDialog(localPanel, "删除成功。"); 
 } 
 }; 
 new Thread(runnable).start(); // 创建并启动这个线程 
 } 
 }
Copy after login

5. Rename local file function

Select the file or folder to be renamed. Clicking the "Rename" button will trigger the actionPerformed(ActionEvent e) method of the com.oyp.ftp.panel.local.RennameAction class. Its main code is as follows


/** 
 * 重命名动作的事件处理方法 
 */ 
 @Override 
 public void actionPerformed(ActionEvent e) { 
 // 获取本地资源表格的选择行号 
 int selRow = this.localPanel.localDiskTable.getSelectedRow(); 
 if (selRow < 0) 
 return; 
 // 获取选择行的第一个单元值 
 Object value = this.localPanel.localDiskTable.getValueAt(selRow, 0); 
 if (!(value instanceof File)) 
 return; 
 // 将该单元值转换为File类的对象 
 File file = (File) value; 
 // 使用对话框接收用户如入的新文件名 
 String fileName = JOptionPane 
 .showInputDialog("请输入新文件名", file.getName()); 
 if (fileName == null) 
 return; 
 // 创建新名称的文件 
 File renFile = new File(file.getParentFile(), fileName); 
 boolean isRename = file.renameTo(renFile); // 将原文件重命名 
 // 刷新文件夹 
 this.localPanel.refreshFolder(file.getParentFile()); 
 if (isRename) { 
 JOptionPane.showMessageDialog(this.localPanel, "重命名为" + fileName 
  + "成功。"); 
 } else { 
 JOptionPane.showMessageDialog(this.localPanel, "无法重命名为" + fileName 
  + "。", "文件重命名", JOptionPane.ERROR_MESSAGE); 
 } 
 }
Copy after login

The above is the detailed content of Implementing FTP local file management module function example using java (ftp software development three). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template