Holger's Java API

## com.antelmann.net
Class FTPConnector

    java.lang.Object
      com.antelmann.net.FTPConnector

All Implemented Interfaces:
  Forgeable<FTPConnector>, LoggerProvider

    @ThirdParty(value="uses org.apache.commons.net.ftp")
    public class FTPConnector

extends Object
implements Forgeable<FTPConnector>, LoggerProvider

FTPConnector implements some useful methods for an FTP client. The methods are synchronized as they use an exclusive connection to the host for performing the operations.

This class uses the package `package org.apache.commons.net.ftp`, which is available at http://jakarta.apache.org/commons/net/index.html.

Alternatively, you may also use the package com.oroinc.net.ftp from the Java NetComponents Library by Original Reusable Objects, which is available at this location. The license for that package is unfortunately NOT GNU, see here for details.

Since:
  11/08/02
Author:
  Holger Antelmann

Field Summary
`static String` `DIR_SEPARATOR`

Constructor Summary
`FTPConnector(FTPConnector ftpc)`
`FTPConnector(String host, String user, char[] passwd)`
`FTPConnector(String host, String user, char[] passwd, boolean localPassiveMode)`
`FTPConnector(String host, String user, String passwd)`
`FTPConnector(String host, String user, String passwd, boolean localPassiveMode)`

Method Summary
`void` `download(String remoteDir, String remoteFileName, OutputStream target)`
`void` `download(String remoteDir, String remoteFileName, OutputStream target, Monitor monitor)` downloads a single file from the given FTP site to the specified target stream.
`void` `downloadFile(String remoteDir, String remoteFileName, File targetFile)`
`void` `downloadFile(String remoteDir, String remoteFileName, File targetFile, Monitor monitor)` downloads a single file from the given FTP site to the specified target file.
`Object[]` `downloadTree(String remoteSourceDir, File targetDir)` recursively downloads the remoteSourceDir tree to the targetDir.
`Object[]` `downloadTree(String remoteSourceDir, File targetDir, Logger logger)` uses a Logger to provide feedback.
`Object[]` `downloadTree(String remoteSourceDir, File targetDir, Logger logger, Monitor monitor)` uses a Monitor for enabling to cancel the operation prematurely.
`org.apache.commons.net.ftp.FTPClient` `getConnectedFTPClient()`
`String` `getHost()`
`Logger` `getLogger()` acess to the (non-null) Logger suitable for logging entries in the context of this instance
`String[]` `getRemoteDirNames(String remoteDir)`
`String[]` `getRemoteFileNames(String remoteDir, boolean includeDirNames)`
`String` `getUser()`
`boolean` `isLocalPassiveMode()`
`void` `setAllFrom(FTPConnector ftpc)` copies the properties of the given object and applies them to this instance, so that its behavior in regards to the given object can be matched to the extend that the implementing class supports that behavior.
`void` `setLocalPassiveMode(boolean localPassiveMode)`
`Object[]` `synchronizeDirectory(File sourceDir, String remoteTargetDir, boolean deleteRemoteFiles)` synchronizes the sourceDir with the remoteTargetDir on the FTP host recursively.
`Object[]` `synchronizeDirectory(File sourceDir, String remoteTargetDir, boolean deleteRemoteFiles, Logger logger)` This method allows for intermediate feedback through the logger; otherwise it's the same as the other synchronizeDirectory() method.
`Object[]` `synchronizeDirectory(File sourceDir, String remoteTargetDir, boolean deleteRemoteFiles, Logger logger, Monitor monitor)` This method allows for intermediate feedback and interactive stopping; otherwise it's the same as the other synchronizeDirectory() method.
`void` `uploadFile(File sourceFile, String targetDir, String targetFileName)` uploads a single local file to the given FTP site using the specified targetDir and targetFileName.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### DIR_SEPARATOR

    public static final String DIR_SEPARATOR

See Also:
  Constant Field Values

Constructor Detail

### FTPConnector

    public FTPConnector(String host,
                        String user,
                        String passwd)

### FTPConnector

    public FTPConnector(String host,
                        String user,
                        char[] passwd)

### FTPConnector

    public FTPConnector(String host,
                        String user,
                        String passwd,
                        boolean localPassiveMode)

### FTPConnector

    public FTPConnector(String host,
                        String user,
                        char[] passwd,
                        boolean localPassiveMode)

### FTPConnector

    public FTPConnector(FTPConnector ftpc)

Method Detail

### getHost

    public String getHost()

### getUser

    public String getUser()

### getLogger

    public Logger getLogger()

  Description copied from interface: `LoggerProvider`
  acess to the (non-null) Logger suitable for logging entries in the context of this instance

  Specified by:
    `getLogger` in interface `LoggerProvider`

### isLocalPassiveMode

    public boolean isLocalPassiveMode()

### setLocalPassiveMode

    public void setLocalPassiveMode(boolean localPassiveMode)

### setAllFrom

    public void setAllFrom(FTPConnector ftpc)

  Description copied from interface: `Forgeable`
  copies the properties of the given object and applies them to this instance, so that its behavior in regards to the given object can be matched to the extend that the implementing class supports that behavior.

  Specified by:
    `setAllFrom` in interface `Forgeable<FTPConnector>`
  Parameters:
    `ftpc` - the object whose behavior is to be copied to this instance

### getConnectedFTPClient

    public org.apache.commons.net.ftp.FTPClient getConnectedFTPClient()
                                                               throws IOException

  Throws:
    `IOException`

### uploadFile

    public void uploadFile(File sourceFile,
                           String targetDir,
                           String targetFileName)
                    throws IOException

  uploads a single local file to the given FTP site using the specified targetDir and targetFileName. This method establishes and closes one connection per call.

  Throws:
    `IOException`

### getRemoteFileNames

    public String[] getRemoteFileNames(String remoteDir,
                                       boolean includeDirNames)
                                throws IOException

  Throws:
    `IOException`

### getRemoteDirNames

    public String[] getRemoteDirNames(String remoteDir)
                               throws IOException

  Throws:
    `IOException`

### downloadFile

    public void downloadFile(String remoteDir,
                             String remoteFileName,
                             File targetFile)
                      throws IOException

  Throws:
    `IOException`

### downloadFile

    public void downloadFile(String remoteDir,
                             String remoteFileName,
                             File targetFile,
                             Monitor monitor)
                      throws IOException

  downloads a single file from the given FTP site to the specified target file. This method establishes and closes one connection per call. An alternative to download a file from an FTP server is to use the class Downloader.

  Throws:
    `IOException`
  See Also:
    `Downloader`

### download

    public void download(String remoteDir,
                         String remoteFileName,
                         OutputStream target)
                  throws IOException

  Throws:
    `IOException`

### download

    public void download(String remoteDir,
                         String remoteFileName,
                         OutputStream target,
                         Monitor monitor)
                  throws IOException

  downloads a single file from the given FTP site to the specified target stream. This method establishes and closes one connection per call.

  Throws:
    `IOException`

### downloadTree

    public Object[] downloadTree(String remoteSourceDir,
                                 File targetDir)
                          throws IOException

  recursively downloads the remoteSourceDir tree to the targetDir. Existing files will be overwritten. Dots or slashes at the beginning of the remoteSourceDir will be ignored. A new FTP connection is opened to perform the operation and closed at the end if the method returns normally.

  Returns:
    array of objects that could not be copied; if no errors occurred, this array would be empty
  Throws:
    `IOException`

### downloadTree

    public Object[] downloadTree(String remoteSourceDir,
                                 File targetDir,
                                 Logger logger)
                          throws IOException

  uses a Logger to provide feedback.

  Returns:
    array of objects that could not be copied; if no errors occurred, this array would be empty
  Throws:
    `IOException`
  See Also:
    `(String, File)`

### downloadTree

    public Object[] downloadTree(String remoteSourceDir,
                                 File targetDir,
                                 Logger logger,
                                 Monitor monitor)
                          throws IOException

  uses a Monitor for enabling to cancel the operation prematurely. The monitor is tested after each logger operation.

  Returns:
    array of objects that could not be copied; if no errors occurred, this array would be empty
  Throws:
    `IOException`
  See Also:
    `(String, File, Logger)`

### synchronizeDirectory

    public Object[] synchronizeDirectory(File sourceDir,
                                         String remoteTargetDir,
                                         boolean deleteRemoteFiles)
                                  throws IOException

  synchronizes the sourceDir with the remoteTargetDir on the FTP host recursively. Dots or slashes at the beginning of the remoteTargetDir will be ignored. A new FTP connection is opened to perform the operation and closed at the end if the method returns normally. The procedure follows the follwing rules:
  - All files that only exist in the first directory, will be copied over
  - If a file already exists at the destination, it will only be overwritten if it is more recent
  - Files at the destination that do not exist at the origin will be deleted if deleteRemoteFiles is true
  - The above will be performed recursively for each subdirectory
  Note that file attributes are ignored, i.e. if a file is copied, the default attributes are kept and may be different from those of the originating file. If a directory is to be removed in the destination dir, the method makes a best effort to do so.

  Returns:
    array of objects that could not be copied or deleted; if no errors occurred, this array would be empty
  Throws:
    `IllegalArgumentException` - if the sourceDir is not a directory
    `IOException`

### synchronizeDirectory

    public Object[] synchronizeDirectory(File sourceDir,
                                         String remoteTargetDir,
                                         boolean deleteRemoteFiles,
                                         Logger logger)
                                  throws IOException

  This method allows for intermediate feedback through the logger; otherwise it's the same as the other synchronizeDirectory() method.

  This method allows e.g. GUIs to display status messages by implementing a special LogWriter.
  The given logger may be null, otherwise its usage is as follows:

  - Every processed directory produces a level Level.FINE message naming the given directory
  - Every copied/deleted file produces a level Level.FINER message naming the file
  - Every unsuccessful copy/delete operation produces a level Level.WARNING message naming the file/directory and containing the IOException as a parameter

  Returns:
    array of objects that could not be copied or deleted; if no errors occurred, this array would be empty
  Throws:
    `IOException`
  See Also:
    `synchronizeDirectory(File, String, boolean)`

### synchronizeDirectory

    public Object[] synchronizeDirectory(File sourceDir,
                                         String remoteTargetDir,
                                         boolean deleteRemoteFiles,
                                         Logger logger,
                                         Monitor monitor)
                                  throws IOException

  This method allows for intermediate feedback and interactive stopping; otherwise it's the same as the other synchronizeDirectory() method.

  This method allows monitoring threads to abort the synchronization by disabling the given monitor during processing. The monitor/logger may be null; monitor checks occur for each directory and after each copy operation.

  Returns:
    array of objects that could not be copied or deleted; if no errors occurred, this array would be empty
  Throws:
    `IOException`
  See Also:
    `synchronizeDirectory(File, String, boolean, Logger)`

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads