org.mortbay.ftp

Class 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.5 2005/06/04 13:38:25 gregwilkins Exp $
Author:
Greg Wilkins

Field Summary

static char
ASCII
static char
BINARY
static char
CARRIAGE_CONTROL
static char
EBCDIC
static char
IMAGE
static char
LOCAL
static char
NON_PRINT
static char
TELNET
static String
anonymous
static int
defaultPort

Constructor Summary

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

Method Summary

void
abort()
Abort transfer command
void
authenticate(String username, String password)
Authenticate User
void
close()
close the FTP session
void
delete(String remoteName)
Delete remote file
String
getLastModifiedTime(String remoteName)
InetAddress
getLocalAddress()
String
getSize(String remoteName)
void
getUrl(String url, OutputStream out)
Get file from a URL spec
Vector
list()
Get list files in remote working directory
Vector
list(String mask)
Get a directory listing from the remote server.
static void
main(String[] args)
void
mkdir(String remoteName)
Create remote directory
void
open(InetAddress hostAddr)
Open connection
void
open(InetAddress hostAddr, int port)
Open connection
void
rename(String oldName, String newName)
Rename remote file
void
rmdir(String remoteName)
Delete remote directory
void
sendFile(String srcName, InetAddress destAddr, int destPort, String username, String password, 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.
InputStream
startGet(String remoteName)
Start get file Start a file transfer remote file to local inputStream.
void
startGet(String remoteName, OutputStream destination)
Start get file Start a file transfer remote file to local file.
void
startGet(String remoteName, String localName)
Start get file Start a file transfer remote file to local file.
void
startPasvGet(String remoteName, OutputStream destination)
void
startPasvGet(String remoteName, String localName)
Start passive get file Start a file transfer remote file to local file.
void
startPasvPut(InputStream source, String remoteName)
void
startPasvPut(String localName, String remoteName)
Start passive put file Start a file transfer local file to input remote file.
void
startPut(InputStream source, String remoteName)
Start put file Start a file transfer local file to input remote file.
OutputStream
startPut(String remoteName)
Start put file Start a file transfer local file to input remote file.
void
startPut(String localName, String remoteName)
Start put file Start a file transfer local file to input remote file.
String
status()
Get remote server status
boolean
transferComplete()
Command complete query
protected void
transferCompleteNotification()
Transfer completion notification.
void
waitUntilTransferComplete()
Wait until Transfer is complete.
String
workingDirectory()
Report remote working directory
void
workingDirectory(String dir)
Set remote working directory

Field Details

ASCII

public static final char ASCII
Field Value:
'A'

BINARY

public static final char BINARY
Field Value:
'I'

CARRIAGE_CONTROL

public static final char CARRIAGE_CONTROL
Field Value:
'C'

EBCDIC

public static final char EBCDIC
Field Value:
'E'

IMAGE

public static final char IMAGE
Field Value:
'I'

LOCAL

public static final char LOCAL
Field Value:
'L'

NON_PRINT

public static final char NON_PRINT
Field Value:
'N'

TELNET

public static final char TELNET
Field Value:
'T'

anonymous

public static final String anonymous

defaultPort

public static final int defaultPort
Field Value:
21

Constructor Details

Ftp

public Ftp()
Ftp constructor

Ftp

public Ftp(InetAddress hostAddr,
           String username,
           String password)
            throws FtpException,
                   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(InetAddress hostAddr,
           int port,
           String username,
           String password)
            throws FtpException,
                   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 Details

abort

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

authenticate

public void authenticate(String username,
                         String password)
            throws FtpException,
                   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

close

public void close()
            throws IOException
close the FTP session

delete

public void delete(String remoteName)
            throws FtpException,
                   IOException
Delete remote file
Parameters:
remoteName - The remote file name
Throws:
FtpException - For local problems or negative server responses

getLastModifiedTime

public String getLastModifiedTime(String remoteName)
            throws FtpException,
                   IOException
Parameters:
remoteName - The remote file name
Returns:
Last modified time string.
Throws:
FtpException - For local problems or negative server responses

getLocalAddress

public InetAddress getLocalAddress()

getSize

public String getSize(String remoteName)
            throws FtpException,
                   IOException
Parameters:
remoteName - The remote file name
Returns:
The size of the remote file
Throws:
FtpException - For local problems or negative server responses

getUrl

public void getUrl(String url,
                   OutputStream out)
            throws FtpException,
                   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

list

public Vector list()
            throws FtpException,
                   IOException
Get list files in remote working directory
Returns:
Array of file names
Throws:
FtpException - For local problems or negative server responses

list

public Vector list(String mask)
            throws FtpException,
                   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(String[] args)

mkdir

public void mkdir(String remoteName)
            throws FtpException,
                   IOException
Create remote directory
Parameters:
remoteName - The remote directory name
Throws:
FtpException - For local problems or negative server responses

open

public void open(InetAddress hostAddr)
            throws FtpException,
                   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(InetAddress hostAddr,
                 int port)
            throws FtpException,
                   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

rename

public void rename(String oldName,
                   String newName)
            throws FtpException,
                   IOException
Rename remote file
Parameters:
oldName - The original file name
newName - The new file name
Throws:
FtpException - For local problems or negative server responses

rmdir

public void rmdir(String remoteName)
            throws FtpException,
                   IOException
Delete remote directory
Parameters:
remoteName - The remote directory name
Throws:
FtpException - For local problems or negative server responses

sendFile

public void sendFile(String srcName,
                     InetAddress destAddr,
                     int destPort,
                     String username,
                     String password,
                     String destName)
            throws FtpException,
                   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

setType

public void setType(char type)
            throws FtpException,
                   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

setType

public void setType(char type,
                    char param)
            throws FtpException,
                   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

setType

public void setType(int length)
            throws FtpException,
                   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

startGet

public InputStream startGet(String remoteName)
            throws FtpException,
                   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(String remoteName,
                     OutputStream destination)
            throws FtpException,
                   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

startGet

public void startGet(String remoteName,
                     String localName)
            throws FtpException,
                   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

startPasvGet

public void startPasvGet(String remoteName,
                         OutputStream destination)
            throws FtpException,
                   IOException

startPasvGet

public void startPasvGet(String remoteName,
                         String localName)
            throws FtpException,
                   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

startPasvPut

public void startPasvPut(InputStream source,
                         String remoteName)
            throws FtpException,
                   IOException

startPasvPut

public void startPasvPut(String localName,
                         String remoteName)
            throws FtpException,
                   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:
localName - Local file name
remoteName - Remote file name
Throws:
FtpException - For local problems or negative server responses

startPut

public void startPut(InputStream source,
                     String remoteName)
            throws FtpException,
                   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:
source -
remoteName - Remote file name
Throws:
FtpException - For local problems or negative server responses

startPut

public OutputStream startPut(String remoteName)
            throws FtpException,
                   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(String localName,
                     String remoteName)
            throws FtpException,
                   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:
localName - Local file name
remoteName - Remote file name
Throws:
FtpException - For local problems or negative server responses

status

public String status()
            throws FtpException,
                   IOException
Get remote server status
Returns:
String description of server status
Throws:
FtpException - For local problems or negative server responses

transferComplete

public boolean transferComplete()
            throws FtpException,
                   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.

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.

waitUntilTransferComplete

public void waitUntilTransferComplete()
            throws FtpException,
                   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

workingDirectory

public String workingDirectory()
            throws FtpException,
                   IOException
Report remote working directory
Returns:
The remote working directory
Throws:
FtpException - For local problems or negative server responses

workingDirectory

public void workingDirectory(String dir)
            throws FtpException,
                   IOException
Set remote working directory
Parameters:
dir - The remote working directory
Throws:
FtpException - For local problems or negative server responses

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