Establish ftp connection : Ftp « Network Protocol « Java






Establish ftp connection

 

import java.io.IOException;

import sun.net.ftp.FtpClient;

public class FtpConnectionDemo {
  public static int BUFFER_SIZE = 10240;

  private FtpClient m_client;

  private String host = "";

  private String user = "";

  private String password = "";

  private String sDir = "";

  public FtpConnectionDemo() {
    try {
      System.out.println("Connecting to host " + host);
      m_client = new FtpClient(host);
      m_client.login(user, password);
      System.out.println("User " + user + " login OK");
      System.out.println(m_client.welcomeMsg);
      m_client.cd(sDir);
      System.out.println("Directory: " + sDir);
      m_client.binary();
      System.out.println("Success.");
    } catch (Exception ex) {
      System.out.println("Error: " + ex.toString());
    }
  }
  protected void disconnect() {
    if (m_client != null) {
      try {
        m_client.closeServer();
      } catch (IOException ex) {
      }
      m_client = null;
    }
  }
}           
         
  








Related examples in the same category

1.Upload file to FTP server
2.Connect to FTP server
3.Get list of files from FTP server
4.Download file from FTP server
5.Delete file from FTP server
6.Running the edtFTPj demo
7.Implements a Java FTP client from socket and RFC
8.Graphical Ftp clientGraphical Ftp client
9.Ftp client demonstration
10.Ftp client gets server file size
11.Use the FTP Client
12.Use the FTPClient: server file transfer
13.A simple Java tftp client