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();
ASCII
public static final char ASCII
BINARY
public static final char BINARY
CARRIAGE_CONTROL
public static final char CARRIAGE_CONTROL
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
anonymous
public static final String anonymous
defaultPort
public static final int defaultPort
abort
public void abort()
throws FtpException,
IOException
Abort transfer command
FtpException
- For local problems or negative server responses
authenticate
public void authenticate(String username,
String password)
throws FtpException,
IOException
Authenticate User
username
- User name for authentication, null implies no user requiredpassword
- Password for authentication, null implies no password
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
remoteName
- The remote file name
FtpException
- For local problems or negative server responses
getLastModifiedTime
public String getLastModifiedTime(String remoteName)
throws FtpException,
IOException
remoteName
- The remote file name
- Last modified time string.
FtpException
- For local problems or negative server responses
getLocalAddress
public InetAddress getLocalAddress()
getSize
public String getSize(String remoteName)
throws FtpException,
IOException
remoteName
- The remote file name
- The size of the remote file
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
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
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.
- Array of file information.
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
remoteName
- The remote directory name
FtpException
- For local problems or negative server responses
open
public void open(InetAddress hostAddr)
throws FtpException,
IOException
Open connection
hostAddr
- The IP address of the remote host
FtpException
- For local problems or negative server responses
open
public void open(InetAddress hostAddr,
int port)
throws FtpException,
IOException
Open connection
hostAddr
- The IP address of the remote hostport
- The port to use for the control connection. The default value is used if the port
is 0.
FtpException
- For local problems or negative server responses
rename
public void rename(String oldName,
String newName)
throws FtpException,
IOException
Rename remote file
oldName
- The original file namenewName
- The new file name
FtpException
- For local problems or negative server responses
rmdir
public void rmdir(String remoteName)
throws FtpException,
IOException
Delete remote directory
remoteName
- The remote directory name
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.
srcName
- Remote file name on source serverdestAddr
- The IP address of the destination hostdestPort
- 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 requiredpassword
- Password for authentication, null implies no password
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.
type
- One of Ftp.ASCII, Ftp.EBCDIC or Ftp.IMAGE
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.
type
- One of Ftp.ASCII or Ftp.EBCDICparam
- One of Ftp.NON_PRINT, Ftp.TELNET or Ftp.CARRIAGE_CONTROL
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.
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.
remoteName
- Remote file name
- InputStream, the data fetched may be read from this inputStream.
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.
remoteName
- Remote file namedestination
- OutputStream to which the received file is written
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.
remoteName
- Remote file namelocalName
- Local file name
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.
remoteName
- Remote file namelocalName
- Local file name
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.
localName
- Local file nameremoteName
- Remote file name
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.
source
- remoteName
- Remote file name
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.
remoteName
- Remote file name
- OutputStream Data written to this output stream is sent to the remote file.
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.
localName
- Local file nameremoteName
- Remote file name
FtpException
- For local problems or negative server responses
status
public String status()
throws FtpException,
IOException
Get remote server status
- String description of server status
FtpException
- For local problems or negative server responses
transferComplete
public boolean transferComplete()
throws FtpException,
IOException
Command complete query
- true if the no outstanding command is in progress, false if there is an outstanding
command or data transfer.
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.
FtpException
- For local problems or negative server responses
workingDirectory
public String workingDirectory()
throws FtpException,
IOException
Report remote working directory
- The remote working directory
FtpException
- For local problems or negative server responses
workingDirectory
public void workingDirectory(String dir)
throws FtpException,
IOException
Set remote working directory
dir
- The remote working directory
FtpException
- For local problems or negative server responses