org.mortbay.ftp

Class Ftp

public class Ftp extends 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 charASCII
static Stringanonymous
static charBINARY
static charCARRIAGE_CONTROL
static intdefaultPort
static charEBCDIC
static charIMAGE
static charLOCAL
static charNON_PRINT
static charTELNET
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
voidabort()
Abort transfer command
voidauthenticate(String username, String password)
Authenticate User
voidclose()
close the FTP session
voiddelete(String remoteName)
Delete remote file
StringgetLastModifiedTime(String remoteName)
InetAddressgetLocalAddress()
StringgetSize(String remoteName)
voidgetUrl(String url, OutputStream out)
Get file from a URL spec
Vectorlist()
Get list files in remote working directory
Vectorlist(String mask)
Get a directory listing from the remote server.
static voidmain(String[] args)
voidmkdir(String remoteName)
Create remote directory
voidopen(InetAddress hostAddr)
Open connection
voidopen(InetAddress hostAddr, int port)
Open connection
voidrename(String oldName, String newName)
Rename remote file
voidrmdir(String remoteName)
Delete remote directory
voidsendFile(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.
voidsetType(char type)
Set the connection data type.
voidsetType(char type, char param)
Set the connection data type.
voidsetType(int length)
Set the connection data type to Local.
voidstartGet(String remoteName, String localName)
Start get file Start a file transfer remote file to local file.
InputStreamstartGet(String remoteName)
Start get file Start a file transfer remote file to local inputStream.
voidstartGet(String remoteName, OutputStream destination)
Start get file Start a file transfer remote file to local file.
voidstartPasvGet(String remoteName, String localName)
Start passive get file Start a file transfer remote file to local file.
voidstartPasvGet(String remoteName, OutputStream destination)
voidstartPasvPut(String localName, String remoteName)
Start passive put file Start a file transfer local file to input remote file.
voidstartPasvPut(InputStream source, String remoteName)
voidstartPut(String localName, String remoteName)
Start put file Start a file transfer local file to input remote file.
OutputStreamstartPut(String remoteName)
Start put file Start a file transfer local file to input remote file.
voidstartPut(InputStream source, String remoteName)
Start put file Start a file transfer local file to input remote file.
Stringstatus()
Get remote server status
booleantransferComplete()
Command complete query
protected voidtransferCompleteNotification()
Transfer completion notification.
voidwaitUntilTransferComplete()
Wait until Transfer is complete.
StringworkingDirectory()
Report remote working directory
voidworkingDirectory(String dir)
Set remote working directory

Field Detail

ASCII

public static final char ASCII

anonymous

public static final String anonymous

BINARY

public static final char BINARY

CARRIAGE_CONTROL

public static final char CARRIAGE_CONTROL

defaultPort

public static final int defaultPort

EBCDIC

public static final char EBCDIC

IMAGE

public static final char IMAGE

LOCAL

public static final char LOCAL

NON_PRINT

public static final char NON_PRINT

TELNET

public static final char TELNET

Constructor Detail

Ftp

public Ftp()
Ftp constructor

Ftp

public Ftp(InetAddress hostAddr, String username, String password)
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)
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

abort

public void abort()
Abort transfer command

Throws: FtpException For local problems or negative server responses

authenticate

public void authenticate(String username, String password)
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()
close the FTP session

Throws: FtpException For local problems or negative server responses

delete

public void delete(String remoteName)
Delete remote file

Parameters: remoteName The remote file name

Throws: FtpException For local problems or negative server responses

getLastModifiedTime

public String getLastModifiedTime(String remoteName)

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)

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)
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()
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)
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)
Create remote directory

Parameters: remoteName The remote directory name

Throws: FtpException For local problems or negative server responses

open

public void open(InetAddress hostAddr)
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)
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)
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)
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)
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)
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 IOException IOException

setType

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

setType

public void setType(int length)
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 IOException IOException

startGet

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

startPasvGet

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

startPasvPut

public void startPasvPut(String localName, String remoteName)
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(InputStream source, String remoteName)

startPut

public void startPut(String localName, String remoteName)
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 OutputStream startPut(String remoteName)
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(InputStream source, String remoteName)
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

status

public String status()
Get remote server status

Returns: String description of server status

Throws: FtpException For local problems or negative server responses

transferComplete

public boolean transferComplete()
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()
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()
Report remote working directory

Returns: The remote working directory

Throws: FtpException For local problems or negative server responses

workingDirectory

public void workingDirectory(String dir)
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.