## org.mortbay.ftp
Class Ftp

    java.lang.Object
      |
      +--org.mortbay.ftp.Ftp

public class Ftp
extends java.lang.Object

FTP Client.

File Transfer Protocol client class. Provides basic FTP client functionality in an Asynchronous interface.

#### Notes

see rfc959.

#### Usage

     Ftp ftp = new Ftp(InetAddress.getByName("RemoteHost"),

                       "TestUser",

                       "TestPass");

     ftp.setType(Ftp.IMAGE);

     ftp.startGet("RemoteFileName","LocalFileName");

     ftp.waitUntilTransferComplete();

     ftp.startPut("LocalFileName","RemoteFileName");

     ftp.waitUntilTransferComplete();

Version:
  $Id: Ftp.java,v 1.2 2003/06/27 19:30:24 hlavac Exp $
Author:
  Greg Wilkins

Field Summary
`static java.lang.String` `anonymous`
`static char` `ASCII`
`static char` `BINARY`
`static char` `CARRIAGE_CONTROL`
`static int` `defaultPort`
`static char` `EBCDIC`
`static char` `IMAGE`
`static char` `LOCAL`
`static char` `NON_PRINT`
`static char` `TELNET`

Constructor Summary
`Ftp()` Ftp constructor
`Ftp(java.net.InetAddress hostAddr, int port, java.lang.String username, java.lang.String password)` Ftp constructor Construct an FTP endpoint, open the command port and authenticate the user.
`Ftp(java.net.InetAddress hostAddr, java.lang.String username, java.lang.String password)` Ftp constructor Construct an FTP endpoint, open the default command port and authenticate the user.

Method Summary
`void` `abort()` Abort transfer command
`void` `authenticate(java.lang.String username, java.lang.String password)` Authenticate User
`void` `close()` close the FTP session
`void` `delete(java.lang.String remoteName)` Delete remote file
`java.lang.String` `getLastModifiedTime(java.lang.String remoteName)`
`java.net.InetAddress` `getLocalAddress()`
`java.lang.String` `getSize(java.lang.String remoteName)`
`void` `getUrl(java.lang.String url, java.io.OutputStream out)` Get file from a URL spec
`java.util.Vector` `list()` Get list files in remote working directory
`java.util.Vector` `list(java.lang.String mask)` Get a directory listing from the remote server.
`static void` `main(java.lang.String[] args)`
`void` `mkdir(java.lang.String remoteName)` Create remote directory
`void` `open(java.net.InetAddress hostAddr)` Open connection
`void` `open(java.net.InetAddress hostAddr, int port)` Open connection
`void` `rename(java.lang.String oldName, java.lang.String newName)` Rename remote file
`void` `rmdir(java.lang.String remoteName)` Delete remote directory
`void` `sendFile(java.lang.String srcName, java.net.InetAddress destAddr, int destPort, java.lang.String username, java.lang.String password, java.lang.String destName)` send file Do a file transfer remote file to remote file on another server.
`void` `setType(char type)` Set the connection data type.
`void` `setType(char type, char param)` Set the connection data type.
`void` `setType(int length)` Set the connection data type to Local.
`java.io.InputStream` `startGet(java.lang.String remoteName)` Start get file Start a file transfer remote file to local inputStream.
`void` `startGet(java.lang.String remoteName, java.io.OutputStream destination)` Start get file Start a file transfer remote file to local file.
`void` `startGet(java.lang.String remoteName, java.lang.String localName)` Start get file Start a file transfer remote file to local file.
`void` `startPasvGet(java.lang.String remoteName, java.io.OutputStream destination)`
`void` `startPasvGet(java.lang.String remoteName, java.lang.String localName)` Start passive get file Start a file transfer remote file to local file.
`void` `startPasvPut(java.io.InputStream source, java.lang.String remoteName)`
`void` `startPasvPut(java.lang.String localName, java.lang.String remoteName)` Start passive put file Start a file transfer local file to input remote file.
`void` `startPut(java.io.InputStream source, java.lang.String remoteName)` Start put file Start a file transfer local file to input remote file.
`java.io.OutputStream` `startPut(java.lang.String remoteName)` Start put file Start a file transfer local file to input remote file.
`void` `startPut(java.lang.String localName, java.lang.String remoteName)` Start put file Start a file transfer local file to input remote file.
`java.lang.String` `status()` Get remote server status
`boolean` `transferComplete()` Command complete query
`void` `waitUntilTransferComplete()` Wait until Transfer is complete.
`java.lang.String` `workingDirectory()` Report remote working directory
`void` `workingDirectory(java.lang.String dir)` Set remote working directory

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

Field Detail

### anonymous

    public static final java.lang.String anonymous

See Also:
  Constant Field Values

### defaultPort

    public static final int defaultPort

See Also:
  Constant Field Values

### ASCII

    public static final char ASCII

See Also:
  Constant Field Values

### LOCAL

    public static final char LOCAL

See Also:
  Constant Field Values

### EBCDIC

    public static final char EBCDIC

See Also:
  Constant Field Values

### IMAGE

    public static final char IMAGE

See Also:
  Constant Field Values

### BINARY

    public static final char BINARY

See Also:
  Constant Field Values

### NON_PRINT

    public static final char NON_PRINT

See Also:
  Constant Field Values

### TELNET

    public static final char TELNET

See Also:
  Constant Field Values

### CARRIAGE_CONTROL

    public static final char CARRIAGE_CONTROL

See Also:
  Constant Field Values

Constructor Detail

### Ftp

    public Ftp()

  Ftp constructor

### Ftp

    public Ftp(java.net.InetAddress hostAddr,
               java.lang.String username,
               java.lang.String password)
        throws FtpException,
               java.io.IOException

  Ftp constructor Construct an FTP endpoint, open the default command port and authenticate the user.

Parameters:
  `hostAddr` - The IP address of the remote host
  `username` - User name for authentication, null implies no user required
  `password` - Password for authentication, null implies no password
Throws:
  `FtpException` - For local problems or negative server responses
  `java.io.IOException`

### Ftp

    public Ftp(java.net.InetAddress hostAddr,
               int port,
               java.lang.String username,
               java.lang.String password)
        throws FtpException,
               java.io.IOException

  Ftp constructor Construct an FTP endpoint, open the command port and authenticate the user.

Parameters:
  `hostAddr` - The IP address of the remote host
  `port` - The port to use for the control connection. The default value is used if the port is 0.
  `username` - User name for authentication, null implies no user required
  `password` - Password for authentication, null implies no password
Throws:
  `FtpException` - For local problems or negative server responses
  `java.io.IOException`

Method Detail

### getLocalAddress

    public java.net.InetAddress getLocalAddress()

### open

    public void open(java.net.InetAddress hostAddr)
              throws FtpException,
                     java.io.IOException

  Open connection

  Parameters:
    `hostAddr` - The IP address of the remote host
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### open

    public void open(java.net.InetAddress hostAddr,
                     int port)
              throws FtpException,
                     java.io.IOException

  Open connection

  Parameters:
    `hostAddr` - The IP address of the remote host
    `port` - The port to use for the control connection. The default value is used if the port is 0.
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### authenticate

    public void authenticate(java.lang.String username,
                             java.lang.String password)
                      throws FtpException,
                             java.io.IOException

  Authenticate User

  Parameters:
    `username` - User name for authentication, null implies no user required
    `password` - Password for authentication, null implies no password
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### setType

    public void setType(char type)
                 throws FtpException,
                        java.io.IOException

  Set the connection data type. The data type is not interpreted by the FTP client.

  Parameters:
    `type` - One of Ftp.ASCII, Ftp.EBCDIC or Ftp.IMAGE
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException` - IOException

### setType

    public void setType(char type,
                        char param)
                 throws FtpException,
                        java.io.IOException

  Set the connection data type. The data type is not interpreted by the FTP client.

  Parameters:
    `type` - One of Ftp.ASCII or Ftp.EBCDIC
    `param` - One of Ftp.NON_PRINT, Ftp.TELNET or Ftp.CARRIAGE_CONTROL
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException` - IOException

### setType

    public void setType(int length)
                 throws FtpException,
                        java.io.IOException

  Set the connection data type to Local. The data type is not interpreted by the FTP client.

  Parameters:
    `length` - Length of word.
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException` - IOException

### transferComplete

    public boolean transferComplete()
                             throws FtpException,
                                    java.io.IOException

  Command complete query

  Returns:
    true if the no outstanding command is in progress, false if there is an outstanding command or data transfer.
  Throws:
    `FtpException` - For local problems or negative server responses. The problem may have been detected before the call to complete during a data transfer, but is only reported when the call to complete is made.
    `java.io.IOException`

### waitUntilTransferComplete

    public void waitUntilTransferComplete()
                                   throws FtpException,
                                          java.io.IOException

  Wait until Transfer is complete. Used to synchronous with an asynchronous transfer. If any exceptions occurred during the transfer, the first exception will be thrown by this method. Multiple threads can wait on the one transfer and all will be given a reference to any exceptions.

  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startGet

    public void startGet(java.lang.String remoteName,
                         java.lang.String localName)
                  throws FtpException,
                         java.io.IOException

  Start get file Start a file transfer remote file to local file. Completion of the transfer can be monitored with the transferComplete() or waitUntilTransferComplete() methods.

  Parameters:
    `remoteName` - Remote file name
    `localName` - Local file name
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startGet

    public java.io.InputStream startGet(java.lang.String remoteName)
                                 throws FtpException,
                                        java.io.IOException

  Start get file Start a file transfer remote file to local inputStream. Completion of the transfer can be monitored with the transferComplete() or waitUntilTransferComplete() methods.

  Parameters:
    `remoteName` - Remote file name
  Returns:
    InputStream, the data fetched may be read from this inputStream.
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startGet

    public void startGet(java.lang.String remoteName,
                         java.io.OutputStream destination)
                  throws FtpException,
                         java.io.IOException

  Start get file Start a file transfer remote file to local file. Completion of the transfer can be monitored with the transferComplete() or waitUntilTransferComplete() methods.

  Parameters:
    `remoteName` - Remote file name
    `destination` - OutputStream to which the received file is written
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startPut

    public void startPut(java.lang.String localName,
                         java.lang.String remoteName)
                  throws FtpException,
                         java.io.IOException

  Start put file Start a file transfer local file to input remote file. Completion of the transfer can be monitored with the transferComplete() or waitUntilTransferComplete() methods.

  Parameters:
    `remoteName` - Remote file name
    `localName` - Local file name
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startPut

    public java.io.OutputStream startPut(java.lang.String remoteName)
                                  throws FtpException,
                                         java.io.IOException

  Start put file Start a file transfer local file to input remote file. Completion of the transfer can be monitored with the transferComplete() or waitUntilTransferComplete() methods.

  Parameters:
    `remoteName` - Remote file name
  Returns:
    OutputStream Data written to this output stream is sent to the remote file.
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startPut

    public void startPut(java.io.InputStream source,
                         java.lang.String remoteName)
                  throws FtpException,
                         java.io.IOException

  Start put file Start a file transfer local file to input remote file. Completion of the transfer can be monitored with the transferComplete() or waitUntilTransferComplete() methods.

  Parameters:
    `remoteName` - Remote file name
    `source` -
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startPasvGet

    public void startPasvGet(java.lang.String remoteName,
                             java.lang.String localName)
                      throws FtpException,
                             java.io.IOException

  Start passive get file Start a file transfer remote file to local file. Completion of the transfer can be monitored with the transferComplete() or waitUntilTransferComplete() methods.

  Parameters:
    `remoteName` - Remote file name
    `localName` - Local file name
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startPasvGet

    public void startPasvGet(java.lang.String remoteName,
                             java.io.OutputStream destination)
                      throws FtpException,
                             java.io.IOException

    `FtpException`
    `java.io.IOException`

### startPasvPut

    public void startPasvPut(java.lang.String localName,
                             java.lang.String remoteName)
                      throws FtpException,
                             java.io.IOException

  Start passive put file Start a file transfer local file to input remote file. Completion of the transfer can be monitored with the transferComplete() or waitUntilTransferComplete() methods.

  Parameters:
    `remoteName` - Remote file name
    `localName` - Local file name
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### startPasvPut

    public void startPasvPut(java.io.InputStream source,
                             java.lang.String remoteName)
                      throws FtpException,
                             java.io.IOException

    `FtpException`
    `java.io.IOException`

### mkdir

    public void mkdir(java.lang.String remoteName)
               throws FtpException,
                      java.io.IOException

  Create remote directory

  Parameters:
    `remoteName` - The remote directory name
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### sendFile

    public void sendFile(java.lang.String srcName,
                         java.net.InetAddress destAddr,
                         int destPort,
                         java.lang.String username,
                         java.lang.String password,
                         java.lang.String destName)
                  throws FtpException,
                         java.io.IOException

  send file Do a file transfer remote file to remote file on another server. This is a synchronous method, unlike startGet and startPut.

  Parameters:
    `srcName` - Remote file name on source server
    `destAddr` - The IP address of the destination host
    `destPort` - The port to use for the control connection. The default value is used if the port is 0.
    `username` - User name for authentication, null implies no user required
    `password` - Password for authentication, null implies no password
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### workingDirectory

    public java.lang.String workingDirectory()
                                      throws FtpException,
                                             java.io.IOException

  Report remote working directory

  Returns:
    The remote working directory
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### workingDirectory

    public void workingDirectory(java.lang.String dir)
                          throws FtpException,
                                 java.io.IOException

  Set remote working directory

  Parameters:
    `dir` - The remote working directory
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### rename

    public void rename(java.lang.String oldName,
                       java.lang.String newName)
                throws FtpException,
                       java.io.IOException

  Rename remote file

  Parameters:
    `oldName` - The original file name
    `newName` - The new file name
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### delete

    public void delete(java.lang.String remoteName)
                throws FtpException,
                       java.io.IOException

  Delete remote file

  Parameters:
    `remoteName` - The remote file name
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### abort

    public void abort()
               throws FtpException,
                      java.io.IOException

  Abort transfer command

  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### list

    public java.util.Vector list()
                          throws FtpException,
                                 java.io.IOException

  Get list files in remote working directory

  Returns:
    Array of file names
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### status

    public java.lang.String status()
                            throws FtpException,
                                   java.io.IOException

  Get remote server status

  Returns:
    String description of server status
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### close

    public void close()
               throws java.io.IOException

  close the FTP session

  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### getUrl

    public void getUrl(java.lang.String url,
                       java.io.OutputStream out)
                throws FtpException,
                       java.io.IOException

  Get file from a URL spec

  Parameters:
    `url` - string of the form: "ftp://username:password@host:port/path/to/file"
    `out` - the OutputStream to place the fetched file in
    `FtpException`
    `java.io.IOException`

### rmdir

    public void rmdir(java.lang.String remoteName)
               throws FtpException,
                      java.io.IOException

  Delete remote directory

  Parameters:
    `remoteName` - The remote directory name
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### getLastModifiedTime

    public java.lang.String getLastModifiedTime(java.lang.String remoteName)
                                         throws FtpException,
                                                java.io.IOException

  Parameters:
    `remoteName` - The remote file name
  Returns:
    Last modified time string.
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### getSize

    public java.lang.String getSize(java.lang.String remoteName)
                             throws FtpException,
                                    java.io.IOException

  Parameters:
    `remoteName` - The remote file name
  Returns:
    The size of the remote file
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### list

    public java.util.Vector list(java.lang.String mask)
                          throws FtpException,
                                 java.io.IOException

  Get a directory listing from the remote server.

  Returns:
    Array of file information.
  Throws:
    `FtpException` - For local problems or negative server responses
    `java.io.IOException`

### main

    public static void main(java.lang.String[] args)

Copyright © 2000 Mortbay Consulting Pty. Ltd. All Rights Reserved.