Home Java javaTutorial Use java to implement FTP upload and download queue window function (ftp software development 2)

Use java to implement FTP upload and download queue window function (ftp software development 2)

Apr 21, 2017 pm 04:49 PM
ftp java

1. First, take a look at the interface of the queue window

Use java to implement FTP upload and download queue window function (ftp software development 2)

2. Take a look at the interface of the upload queue window

Use java to implement FTP upload and download queue window function (ftp software development 2)

3. Take a look at the interface of the download queue window

Use java to implement FTP upload and download queue window function (ftp software development 2)


package com.oyp.ftp.panel.queue; 
 
import static java.awt.BorderLayout.CENTER; 
import static java.awt.BorderLayout.EAST; 
import static javax.swing.ListSelectionModel.SINGLE_SELECTION; 
 
import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 
import java.util.LinkedList; 
 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JToggleButton; 
import javax.swing.JToolBar; 
import javax.swing.ListSelectionModel; 
import javax.swing.Timer; 
import javax.swing.table.DefaultTableModel; 
 
import com.oyp.ftp.FTPClientFrame; 
import com.oyp.ftp.utils.FtpClient; 
import com.oyp.ftp.utils.FtpFile; 
 
/** 
 * 任务队列控制面板 
 */ 
public class QueuePanel extends JPanel implements ActionListener { 
 private JTable queueTable = new JTable(); // 显示任务队列的表格组件 
 private JScrollPane scrollPane = new JScrollPane(); 
 private FTPClientFrame frame; // 主窗体的引用对象 
 private String[] columns; 
 private FtpClient ftpClient; // FTP协议的控制类 
 private Timer queueTimer;  // 队列的定时器 
 private LinkedList<Object[]> localQueue; // 本地面板的上传队列 
 private LinkedList<Object[]> ftpQueue;  // FTP面板的下载队列 
 private JToggleButton stopButton; 
 private boolean stop = false; // 队列的控制变量 
 
 /** 
  * 默认的构造方法 
  */ 
 public QueuePanel() { 
  initComponent(); 
 } 
 
 /** 
  * 自定义的构造方法 
  * 
  * @param frame 
  *   主窗体 
  */ 
 public QueuePanel(FTPClientFrame frame) { 
  this.frame = frame; 
  // 从主窗体获取本地面板的上传队列 
  localQueue = (LinkedList<Object[]>) frame.getLocalPanel().getQueue(); 
  // 从主窗体获取FTP面板的下载队列 
  ftpQueue = (LinkedList<Object[]>) frame.getFtpPanel().getQueue(); 
  initComponent(); // 初始化窗体界面 
  // 创建定时器,每间隔1秒执行队列刷新任务 
  queueTimer = new Timer(1000, new ActionListener() { 
   /** 
    * 定时器的事件处理方法 
    */ 
   @Override 
   public void actionPerformed(ActionEvent e) { 
    if (localQueue.size() + ftpQueue.size() == queueTable 
      .getRowCount()) // 如果队列大小没有改变 
     return; // 结束本方法,不做任何操作 
    refreshQueue(); // 否则刷新显示队列信息的表格数据 
   } 
  }); 
 } 
 
 private void initComponent() { 
  BorderLayout cardLayout = new BorderLayout(); 
  setLayout(cardLayout); 
  columns = new String[] { "任务名称", "方向", "主机", "执行状态" }; 
  queueTable.setModel(new DefaultTableModel(new Object[][] {}, columns)); 
  queueTable.getTableHeader().setReorderingAllowed(false); 
  scrollPane.setViewportView(queueTable); 
  cardLayout.layoutContainer(scrollPane); 
  add(scrollPane, CENTER); 
 
  JToolBar controlTool = new JToolBar(JToolBar.VERTICAL); 
  controlTool.setRollover(true); 
  controlTool.setFloatable(false); 
  JButton upButton = new JButton("上移"); 
  upButton.setActionCommand("up"); 
  upButton.addActionListener(this); 
  JButton downButton = new JButton("下移"); 
  downButton.setActionCommand("down"); 
  downButton.addActionListener(this); 
  stopButton = new JToggleButton("暂停"); 
  stopButton.setActionCommand("stop&start"); 
  stopButton.addActionListener(this); 
  JButton delButton = new JButton("删除"); 
  delButton.setActionCommand("del"); 
  delButton.addActionListener(this); 
  JButton clearButton = new JButton("清空"); 
  clearButton.setActionCommand("clear"); 
  clearButton.addActionListener(this); 
  controlTool.setLayout(new BoxLayout(controlTool, BoxLayout.Y_AXIS)); 
  controlTool.add(upButton); 
  controlTool.add(downButton); 
  controlTool.add(stopButton); 
  controlTool.add(delButton); 
  controlTool.add(clearButton); 
  add(controlTool, EAST); 
 } 
 
 public void startQueue() { 
  ftpClient = frame.getFtpClient(); 
  queueTimer.start(); 
 } 
 
 /** 
  * 界面上按钮的事件处理方法 
  */ 
 @Override 
 public void actionPerformed(ActionEvent e) { 
  final String command = e.getActionCommand(); 
  if (command.equals("stop&start")) {// 处理暂停按钮事件 
   if (stopButton.isSelected()) { 
    stop = true; 
    stopButton.setText("继续"); 
   } else { 
    stop = false; 
    stopButton.setText("暂停"); 
   } 
  } 
  // 处理上移和下移按钮事件 
  if (command.equals("up") || command.equals("down")) { 
   up_Down_Action(command); // 调用处理上移和下移动作的方法 
  } 
  if (command.equals("del")) {// 处理删除按钮的事件 
   int row = queueTable.getSelectedRow(); // 获取显示队列的表格的当前选择行 
   if (row < 0) 
    return; 
   // 获取选择行的第一个表格单元值 
   Object valueAt = queueTable.getValueAt(row, 0); 
   // 如果选择内容是File类的对象 
   if (valueAt instanceof File) { 
    File file = (File) valueAt; 
    int size = localQueue.size(); // 获取上传队列大小 
    for (int i = 0; i < size; i++) { // 遍历上传队列 
     if (localQueue.get(i)[0].equals(file)) { 
      localQueue.remove(i); // 从上传队列中删除文件对象 
     } 
    } 
   } else if (valueAt instanceof String) { // 如果选择的是字符串对象 
    String fileStr = (String) valueAt; 
    int size = ftpQueue.size(); // 获取上传队列的大小 
    for (int i = 0; i < size; i++) { // 遍历上传队列 
     // 获取上传队列中的文件对象 
     FtpFile ftpFile = (FtpFile) ftpQueue.get(i)[0]; 
     if (ftpFile.getAbsolutePath().equals(fileStr)) { 
      ftpQueue.remove(i); // 从上传队列中删除该文件对象 
     } 
    } 
   } 
   refreshQueue(); // 刷新队列列表 
  } 
  if (command.equals("clear")) { // 处理清空按钮的事件 
   localQueue.clear(); // 调用本地面板的队列的clear()方法 
   ftpQueue.clear(); // 调用FTP面板的队列的clear()方法 
  } 
 } 
 
 /** 
  * 队列任务的上移和下移动作处理方法 
  * 
  * @param command 
  *   上移或下移命令 
  */ 
 private void up_Down_Action(final String command) { 
  int row = queueTable.getSelectedRow(); // 获取队列表格的当前选择行号 
  if (row < 0) 
   return; 
  // 获取当前选择行的第一个单元值 
  Object valueAt = queueTable.getValueAt(row, 0); 
  // 获取当前选择行的第二个单元值作为判断上传或下载方向的依据 
  String orientation = (String) queueTable.getValueAt(row, 1); 
  if (orientation.equals("上传")) { // 如果是上传任务 
   String value = ((File) valueAt).getAbsolutePath(); 
   int size = localQueue.size(); 
   for (int i = 0; i < size; i++) { // 遍历上传队列 
    Object[] que = localQueue.get(i); 
    File file = (File) que[0]; 
    // 从队列中,遍历到选择的上传任务的文件对象 
    if (file.getAbsolutePath().equals(value)) { 
     ListSelectionModel selModel = queueTable 
       .getSelectionModel(); // 获取表格的选择模型 
     selModel // 设置选择模型的单选模式 
       .setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     int dsize = localQueue.size(); // 获取本地上传队列的大小 
     int drow = queueTable.getSelectedRow();// 获取队列表格的当前选择行号 
     int queueVal = localQueue.size() - dsize; 
 
     int next = -1; 
     int selIndex = -1; 
     if (command.equals("up")) { 
      if (i < 1) // 限制选择范围 
       return; 
      selIndex = drow - queueVal - 1; 
      next = i - 1; 
     } else { 
      if (i >= size - 1) // 限制选择范围 
       return; 
      selIndex = drow - queueVal + 1; 
      next = i + 1; 
     } 
     // 交换连个队列位置的任务 
     Object[] temp = localQueue.get(next); 
     localQueue.set(next, que); 
     localQueue.set(i, temp); 
     refreshQueue(); // 刷新队列表格的列表 
     // 设置表格选择第一行 
     selModel.setSelectionInterval(0, selIndex); 
     break; 
    } 
   } 
  } else if (orientation.equals("下载")) { // 如果是下载任务 
   String value = (String) valueAt; 
   int size = ftpQueue.size(); // 获取FTP下载队列的大小 
   for (int i = 0; i < size; i++) { // 遍历下载队列 
    Object[] que = ftpQueue.get(i); 
    FtpFile file = (FtpFile) que[0]; // 获取每个下载任务的FTP文件对象 
    if (file.getAbsolutePath().equals(value)) {// 
     ListSelectionModel selModel = queueTable 
       .getSelectionModel(); // 获取任务队列表格的选择模型 
     // 设置模型使用单选模式 
     selModel.setSelectionMode(SINGLE_SELECTION); 
     int dsize = ftpQueue.size(); 
     int drow = queueTable.getSelectedRow(); 
     int queueVal = ftpQueue.size() - dsize; 
 
     int next = -1; 
     int selIndex = -1; 
     if (command.equals("up")) { 
      if (i < 1) // 限制选择范围 
       return; 
      selIndex = drow - queueVal - 1; 
      next = i - 1; 
     } else { 
      if (i >= size - 1) // 限制选择范围 
       return; 
      selIndex = drow - queueVal + 1; 
      next = i + 1; 
     } 
     // 交换连个队列位置的任务内容 
     Object[] temp = ftpQueue.get(next); 
     ftpQueue.set(next, que); 
     ftpQueue.set(i, temp); 
     refreshQueue(); // 刷新任务队列的表格列表 
     // 选择表格的第一行 
     selModel.setSelectionInterval(0, selIndex); 
     break; 
    } 
   } 
  } 
 } 
 
 /** 
  * 刷新队列的方法 
  */ 
 private synchronized void refreshQueue() { 
  // 如果自动关机按钮被按下并且上传和下载的队列都有任务 
  if (frame.getShutdownButton().isSelected() && localQueue.isEmpty() 
    && ftpQueue.isEmpty()) { 
   try { 
    // 执行系统关机命令,延迟30秒钟 
    Runtime.getRuntime().exec("shutdown -s -t 30"); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
  } 
  // 创建表格的数据模型对象 
  DefaultTableModel model = new DefaultTableModel(columns, 0); 
  // 获取本地上传队列中的任务 
  Object[] localQueueArray = localQueue.toArray(); 
  // 遍历本地上传任务 
  for (int i = 0; i < localQueueArray.length; i++) { 
   Object[] queueValue = (Object[]) localQueueArray[i]; 
   if (queueValue == null) 
    continue; 
   File localFile = (File) queueValue[0]; 
   // 把上传队列的任务添加到表格组件的数据模型中 
   model.addRow(new Object[] { localFile.getAbsoluteFile(), "上传", 
     ftpClient.getServer(), i == 0 ? "正在上传" : "等待上传" }); 
  } 
  // 获取下载队列的任务 
  Object[] ftpQueueArray = ftpQueue.toArray(); 
  // 遍历下载队列 
  for (int i = 0; i < ftpQueueArray.length; i++) { 
   Object[] queueValue = (Object[]) ftpQueueArray[i]; 
   if (queueValue == null) 
    continue; 
   FtpFile ftpFile = (FtpFile) queueValue[0]; 
   // 把下载队列的任务添加到表格组件的数据模型中 
   model.addRow(new Object[] { ftpFile.getAbsolutePath(), "下载", 
     ftpClient.getServer(), i == 0 ? "正在下载" : "等待下载" }); 
  } 
  queueTable.setModel(model); // 设置表格使用本方法的表格数据模型 
 } 
 
 public boolean isStop() { 
  return stop; 
 } 
}
Copy after login

5. Implementation of the upload queue window

package com.oyp.ftp.panel.queue; 
 
import java.awt.CardLayout; 
 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableColumn; 
 
import com.oyp.ftp.panel.QueueTableCellRanderer; 
import com.oyp.ftp.utils.ProgressArg; 
 
public class UploadPanel extends JPanel { 
 private JTable uploadTable = new JTable(); // 表格组件 
 private JScrollPane scrollPane = new JScrollPane(); 
 private DefaultTableModel model; // 表格的数据模型 
 
 /** 
  * 构造方法,用于初始化程序界面 
  */ 
 public UploadPanel() { 
  CardLayout cardLayout = new CardLayout(); 
  setLayout(cardLayout); 
  ProgressArg progressArg = new ProgressArg(-1, -1, -1); 
  model = new DefaultTableModel(new Object[][] { new Object[] { "", "", 
    "", "", progressArg } }, new String[] { "文件名", "大小", "远程文件名", 
    "主机", "状态" }); 
  uploadTable.setModel(model); 
  uploadTable.getTableHeader().setReorderingAllowed(false); 
  uploadTable.setRowSelectionAllowed(false); 
  TableColumn column = uploadTable.getColumn("状态"); 
  column.setCellRenderer(new QueueTableCellRanderer()); 
  scrollPane.setViewportView(uploadTable); 
  cardLayout.layoutContainer(scrollPane); 
  add(scrollPane, "queue"); 
 } 
 
 /** 
  * 向上传队列的表格组件添加新任务的方法 
  * 
  * @param values 
  *   - 添加到表格的一行数据的数组对象 
  */ 
 public void addRow(final Object[] values) { 
  Runnable runnable = new Runnable() { 
   public void run() { 
    model.insertRow(0, values); // 向表格的数据模型添加数据 
   } 
  }; 
  if (SwingUtilities.isEventDispatchThread()) 
   runnable.run(); // 在事件队列执行 
  else 
   SwingUtilities.invokeLater(runnable); // 或有事件队列调用 
 } 
}
Copy after login

6. Implementation of the download queue window

package com.oyp.ftp.panel.queue; 
 
import java.awt.CardLayout; 
 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableColumn; 
 
import com.oyp.ftp.panel.QueueTableCellRanderer; 
import com.oyp.ftp.utils.ProgressArg; 
 
public class DownloadPanel extends JPanel { 
 private JTable downloadTable = new JTable(); 
 private JScrollPane scrollPane = new JScrollPane(); 
 private DefaultTableModel model; 
 
 public DownloadPanel() { 
  CardLayout cardLayout = new CardLayout(); 
  setLayout(cardLayout); 
  ProgressArg progressArg = new ProgressArg(-1, -1, -1); 
  model = new DefaultTableModel(new Object[][] { new Object[] { "", "", 
    "", "", progressArg } }, new String[] { "文件名", "大小", "本地文件名", 
    "主机", "状态" }); 
  downloadTable.setModel(model); 
  downloadTable.getTableHeader().setReorderingAllowed(false); 
  downloadTable.setRowSelectionAllowed(false); 
  TableColumn column = downloadTable.getColumn("状态"); 
  column.setCellRenderer(new QueueTableCellRanderer()); 
  scrollPane.setViewportView(downloadTable); 
  cardLayout.layoutContainer(scrollPane); 
  add(scrollPane, "queue"); 
 } 
 
 public void addRow(final Object[] values) { 
  Runnable runnable = new Runnable() { 
   public void run() { 
    model.insertRow(0, values); 
   } 
  }; 
  if (SwingUtilities.isEventDispatchThread()) 
   runnable.run(); 
  else 
   SwingUtilities.invokeLater(runnable); 
 } 
}
Copy after login

The specific changes in the following window after uploading and downloading

1. Adding the upload task

Use java to implement FTP upload and download queue window function (ftp software development 2)

##2. Completion of the upload task

Use java to implement FTP upload and download queue window function (ftp software development 2)

3. Addition of the download task

Use java to implement FTP upload and download queue window function (ftp software development 2)

4. Completion of the download task

Use java to implement FTP upload and download queue window function (ftp software development 2)

The above is the detailed content of Use java to implement FTP upload and download queue window function (ftp software development 2). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles