users
[Top] [All Lists]

RE: [cinjug-users] FTP

To: "CinJUG (E-mail)" <users@xxxxxxxxxx>
Subject: RE: [cinjug-users] FTP
From: "Chip Dunning" <cdunning@xxxxxxxxxxxx>
Date: Fri, 9 Jul 2004 11:58:36 -0400
Delivered-to: mailing list users@cinjug.org
Mailing-list: contact users-help@cinjug.org; run by ezmlm
Thread-index: AcRlyYUIcToC3q2dS5e5Xy5buin7tAAASClw
Thread-topic: [cinjug-users] FTP
I tried both the core libraries and the ones in the Apache Commons project. I 
got the same results from both sets. Currently I am using the Apache Commons 
version because it was more stable for connections over time. The JVM is 
1.4.2_03 and the OS is Windows2000 on both ends.
        All of the PCs involved use MS basic FTP capability except for one 
client which uses Serv-U so that we can limit its available bandwidth as other 
more important traffic also flows down the same pipe.  We checked that one slow 
pipe wasn't messing up the work for all involved by only FTPing along the 100Mb 
local LAN (and its still 3x slower than a simple Command FTP).

I have include a partial version of the class below that does the actual FTP 
work. We FTP into a temp directory, then execute a rename command to move the 
file into the correct directory. This is because we have a client on the other 
side scanning this directory for files - which it will place into the correct 
local location.


Chip
-----
"The reason the mainstream is thought of as a stream is because it's so 
shallow" --George Carlin
 

===============[ code snippet ]=======================

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;


/**
 * @author cdunning
 * @version 0.0
 *
 * This product includes software developed by the
 * Apache Software Foundation (http://www.apache.org/).
 */
public class FTPMover extends Thread
    implements Observer {

   [snipped a bunch of setup/database stuff]


  /** Transfer the file
   *  @param srcFile File to ftp
   *  @return true is the ftp is successful
   */
  private boolean ftpFile(File srcFile) {
    theLogger.finest("Attempting to ftp " + srcFile.getName());
    boolean status = false;

    FTPClient ftp = new FTPClient();

    DataModel dm = Toolbox.getInstance().getDataModel();
    String rcvdPath = dm.getReceiveDirectory();
    String tempPath = dm.getTempDirectory();

    try {
      ftp.setDefaultTimeout(dm.getFtpTimeout());
      ftp.enterLocalActiveMode();

      ftp.connect(ipAddress, 21);
      customerData.setReplayStatus("Connected");

      if (!ftp.login(dm.getFTPUser(), dm.getFTPPassword())) {
        ftp.logout();
        customerData.setReplayStatus("Login Failed");
        theLogger.log(Level.SEVERE, ipAddress + " failed login");
        throw new IOException();
      }
      customerData.setReplayStatus("Logged In");

      ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
      ftp.setFileType(FTP.BINARY_FILE_TYPE);
      ftp.setSoTimeout(600000);

      if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
        ftp.logout();
        System.err.println("FTP server refused connection.");
        customerData.setReplayStatus("Disconnected");
        theLogger.log(Level.SEVERE, ipAddress
                      + " refused ftp connection");
        throw new IOException();
      }

      customerData.setReplayStatus("FTP " + srcFile.getName());
      InputStream input = new FileInputStream(srcFile);
      ftp.storeFile(tempPath + File.separator + srcFile.getName(),
                    input);
      input.close();
      customerData.setReplayStatus("FTP Successful");

      sleep(1500);
      ftp.rename(tempPath + File.separator + srcFile.getName(),
                 rcvdPath + File.separator + srcFile.getName());
      ftp.logout();
      customerData.setReplayStatus("Logged Out");
      status = true;

    } catch (InterruptedIOException e) {
      theLogger.log(Level.SEVERE, ipAddress + " FTP Timeout ("
                    + e.bytesTransferred + " bytes transferred)", e);
      errorLog.append("FTP Timeout (" + srcFile.getName() + ")\n");
      status = false;

    } catch (FTPConnectionClosedException ex) {
      theLogger.log(Level.SEVERE, ipAddress + " severed connection on "
                    + srcFile, ex);
      errorLog.append("Server FTP closed connection ("
                      + srcFile.getName() + ")\n");
      status = false;

    } catch (IOException ex) {
      customerData.setReplayStatus("Error");
      theLogger.log(Level.SEVERE, ipAddress + " connection failure on "
                    + srcFile.getName(), ex);
      errorLog.append("FTP Connection Error (" + srcFile.getName()
                      + ")\n");
      status = false;

    } catch (InterruptedException e) {
      theLogger.log(Level.FINER, ipAddress + " sleep disrupted");
      status = false;

    } finally {
      if (ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch (IOException f) {
          theLogger.warning(ipAddress + " ftp failed to disconnect");
        }
      }
    }

    return status;
  }


  /**
   * Depending on what happened during the last time this program
   * was run it may have left files in the temp directory. This
   * will clean up any files left behind.
   *
   * @return true if all the temporary directory is clean after this runs
   */
  private boolean cleanupTempDirectory() {
    FTPClient ftp = new FTPClient();

    try {
      DataModel dm = Toolbox.getInstance().getDataModel();

      ftp.setDefaultTimeout(dm.getFtpTimeout());
      ftp.enterLocalActiveMode();

      ftp.connect(ipAddress, 21);
      customerData.setReplayStatus("Connected");

      if (!ftp.login(dm.getFTPUser(), dm.getFTPPassword())) {
        ftp.logout();
        customerData.setReplayStatus("Login Failed");
        theLogger.log(Level.SEVERE, ipAddress + " failed login");
        throw new IOException();
      }
      customerData.setReplayStatus("Logged In");

      ftp.setSoTimeout(60000);

      if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
        ftp.logout();
        System.err.println("FTP server refused connection.");
        customerData.setReplayStatus("Disconnected");
        theLogger.log(Level.SEVERE, ipAddress
                      + " refused ftp connection");
        throw new IOException();
      }

      ftp.changeWorkingDirectory(dm.getTempDirectory());
      String[] files = ftp.listNames();
      customerData.setReplayStatus("Cleaning temp");

      for (int i = 0; i < files.length; i++) {
        ftp.deleteFile(files[i]);
        theLogger.log(Level.INFO, ipAddress + " Deleting " + files[i]
                      + " from temp directory\n");
      }

    } catch (InterruptedIOException e) {
      theLogger.log(Level.SEVERE, "FTP Timeout", e);

    } catch (IOException ex) {
      errorLog.append("FTP IO error in temp directory\n");
      theLogger.log(Level.SEVERE, ipAddress + " IOError", ex);
      return false;

    } finally {
      customerData.setReplayStatus("Logging out");
      if (ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch (IOException f) {
          theLogger.warning(ipAddress + " ftp failed to disconnect");
        }
      }
    }

    customerData.setReplayStatus("Finished");
    return true;
  }

}

<Prev in Thread] Current Thread [Next in Thread>