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 2001/11/30 16:12:00 kobit 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()
           
 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.
 java.io.InputStream startPasvGet(java.lang.String remoteName)
           
 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
protected  void transferCompleteNotification()
          Transfer completion notification.
 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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

anonymous

public static final java.lang.String anonymous

defaultPort

public static final int defaultPort

ASCII

public static final char ASCII

LOCAL

public static final char LOCAL

EBCDIC

public static final char EBCDIC

IMAGE

public static final char IMAGE

BINARY

public static final char BINARY

NON_PRINT

public static final char NON_PRINT

TELNET

public static final char TELNET

CARRIAGE_CONTROL

public static final char CARRIAGE_CONTROL
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

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
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

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

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

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.

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

transferCompleteNotification

protected void transferCompleteNotification()
Transfer completion notification. This protected member can be overridden in a derived class as an alternate notification mechanism for transfer completion. Default implementation does nothing.

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

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

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

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

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

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
localName - Local file name
Throws:
FtpException - For local problems or negative server responses

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

startPasvGet

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

startPasvGet

public void startPasvGet(java.lang.String remoteName,
                         java.io.OutputStream destination)
                  throws 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

startPasvPut

public void startPasvPut(java.io.InputStream source,
                         java.lang.String remoteName)
                  throws 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

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
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

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

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

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

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

abort

public void abort()
           throws FtpException,
                  java.io.IOException
Abort transfer command
Throws:
FtpException - For local problems or negative server responses

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

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

close

public void close()
           throws java.io.IOException
close the FTP session
Throws:
FtpException - For local problems or negative server responses

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

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

getLastModifiedTime

public java.lang.String getLastModifiedTime(java.lang.String remoteName)
                                     throws FtpException,
                                            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

main

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


Copyright GNU, wttools developers Team.