Home  >  Article  >  Java  >  Use java to make a simple FTP connection management module (ftp software development 1)

Use java to make a simple FTP connection management module (ftp software development 1)

Y2J
Y2JOriginal
2017-04-21 16:42:452264browse

This article introduces the implementation method of the FTP connection management module for your reference. The specific content is as follows

(1) FTP connection

Run the FTP client Finally, the first thing is to connect to the FTP server. You need to enter the IP address, user name, password and port number of the FTP server and click the connect button to start connecting to the FTP server. The connection flow chart is shown in the figure below.

After clicking the "Connect" button, the linkButtonActionPerformed(ActionEvent evt) method of the com.oyp.ftp.FTPClientFrame class will be called. The main code program is as follows

/** 
* 连接按钮的事件处理方法 
*/ 
rivate void linkButtonActionPerformed(java.awt.event.ActionEvent evt) { 
try { 
  String server = serverTextField.getText(); // 获取服务器地址 
  if (server == null) { 
    return; 
  } 
  String portStr = portTextField.getText(); // 获取端口号 
  if (portStr == null) { 
    portStr = "21"; 
  } 
  int port = Integer.parseInt(portStr.trim()); 
  String userStr = userTextField.getText(); // 获取用户名 
  userStr = userStr == null ? "" : userStr.trim(); 
  String passStr = PassField.getText(); // 获取密码 
  passStr = passStr == null ? "" : passStr.trim(); 
  cutLinkButton.doClick(); 
  ftpClient = new FtpClient(); 
  ftpClient.openServer(server.trim(), port); // 连接服务器 
  ftpClient.login(userStr, passStr); // 登录服务器 
  ftpClient.binary(); // 使用二进制传输模式 
  if (ftpClient.serverIsOpen()) { // 如果连接成功 
    CUT_LINK_ACTION.setEnabled(true); // 设置断开按钮可用 
  } else { // 否则 
    CUT_LINK_ACTION.setEnabled(false); // 设置断开按钮不可用 
    return; // 并结束事件处理 
  } 
  // 设置本地资源管理面板的FTP连接信息 
  localPanel.setFtpClient(server, port, userStr, passStr); 
  // 设置上传按钮可用 
  localPanel.getActionMap().get("uploadAction").setEnabled(true); 
  ftpPanel.setFtpClient(ftpClient);// 设置FTP资源管理面板的FTP连接信息 
  // 设置下载按钮可用 
  ftpPanel.getActionMap().get("downAction").setEnabled(true); 
  ftpPanel.refreshCurrentFolder();// 刷新FTP资源管理面板的当前文件夹 
  queuePanel.startQueue(); // 启动任务队列线程 
} catch (Exception ex) { 
  ex.printStackTrace(); 
}

(2) FTP Disconnect

Click the "Disconnect" button to stop the upload thread, stop the download thread, clear the task queue, clear the contents of the FTP resource table, clear the queue of the local panel, etc. The flow chart of the disconnection terminal connection module is shown in the figure.


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

/** 
 * 处理断开按钮的按钮动作事件的方法 
 */ 
@Override 
public void actionPerformed(ActionEvent e) { 
  try { 
    frame.ftpPanel.stopDownThread(); // 停止下载线程 
    frame.localPanel.stopUploadThread(); // 停止上传线程 
    frame.getFtpPanel().getQueue().clear(); // 清空任务队列 
    frame.getFtpPanel().clearTable(); // 清除FTP资源表格内容 
    frame.getLocalPanel().getQueue().clear(); // 清除本地面板的队列 
    // 如果FTP连接对象存在,并且已经连接FTP服务器 
    if (frame.ftpClient != null && frame.ftpClient.serverIsOpen()) { 
      frame.ftpClient.sendServer("quit\r\n"); // 发送断开连接的FTP协议的命令 
      frame.ftpClient.readServerResponse(); // 读取返回编码 
      frame.ftpClient = null; 
    } 
    // 设置上传按钮不可用 
    frame.localPanel.getActionMap().get("uploadAction").setEnabled( 
        false); 
    // 设置下载按钮不可用 
    frame.ftpPanel.getActionMap().get("downAction").setEnabled(false); 
    setEnabled(false); // 设置本按钮(断开)不可用 
  } catch (IOException e1) { 
    e1.printStackTrace(); 
  } 
}

The above is the detailed content of Use java to make a simple FTP connection management module (ftp software development 1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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