home : docs : ftp client user's guide
examples of how to use the ftp client objects


snippet variables

  FtpClient ftp = new FtpClient();
  FtpResponse resp;
  FtpReader ftpin;
  FtpWriter ftpout;

connecting to a server

  // connect to server
  ftp.connect("prep.ai.mit.edu");
  resp = ftp.getResponse();
  if (!resp.isPositiveCompletion()) {
      ...oops...
  }
  // send user name ...
  ftp.userName("anonymous");
  resp = ftp.getResponse();
  if (!resp.isPositiveIntermediary()) {
      ...oops...
  }
  // ... and password
  ftp.password("a@b.c");
  resp = ftp.getResponse();
  if (!resp.isPositiveCompletion()) {
      ...oops...
  }
back to top

getting a listing

  // set type to ASCII
  ftp.representationType(ASCII_TYPE);
  resp = ftp.getResponse();
  if (!resp.isPositiveCompletion()) {
      ...oops...
  }
  // create port to which server can send data
  ftp.dataPort();
  resp = ftp.getResponse();
  if (!resp.isPositiveCompletion()) {
      ...oops...
  }
  // request listing ...
  ftpin = ftp.list();
  resp = ftp.getResponse();
  if (!resp.isPositivePreliminary()) {
      ...oops...
  }
  // ... process listing
  while (ftpin.ready())
    System.out.println(ftpin.readLine());
  resp = ftp.getResponse();
  if (!resp.isPositivePreliminary()) {
      ...oops...
  }
  // explicit close to let FtpClient know we're done
  ftpin.close();
  resp = ftp.getResponse();
  if (!resp.isPositiveCompletion()) {
      ...oops...
  }
back to top

sending a file

  // request to send file ...
  ftpout = ftp.store("foo.txt");
  resp = ftp.getResponse();
  if (!resp.isPositivePreliminary()) {
      ...oops...
  }
  // ... send file data
  for (int i=1; i<200; ++i) {
    ftpout.write("This is line number " + i + " of 199");
    ftpout.newLine();
  }
  // explicit close to let FtpClient know we're done
  ftpout.close();
  resp = ftp.getResponse();
  if (!resp.isPositiveCompletion()) {
      ...oops...
  }
back to top

logging out

  ftp.logout();
  resp = ftp.getResponse();
  if (!resp.isPositiveCompletion()) {
      ...oops...
  }
back to top