APIdia Launch Partner
Top Description Inners Fields Constructors Methods
sun.net.ftp

public abstract Class FtpClient

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
sun.net.ftp.impl.FtpClient
Imports
java.net.*, java.io.*, java.util.Date, .List, .Iterator

A class that implements the FTP protocol according to RFCs 959, 2228, 2389, 2428, 3659, 4217. Which includes support for FTP over SSL/TLS (aka ftps). FtpClient provides all the functionalities of a typical FTP client, like storing or retrieving files, listing or creating directories. A typical usage would consist of connecting the client to the server, log in, issue a few commands then logout. Here is a code example:
FtpClient cl = FtpClient.create();
cl.connect("ftp.gnu.org").login("anonymous", "[email protected]".toCharArray())).changeDirectory("pub/gnu");
Iterator<FtpDirEntry> dir = cl.listFiles();
    while (dir.hasNext()) {
        FtpDirEntry f = dir.next();
        System.err.println(f.getName());
    }
    cl.close();
}

Error reporting: There are, mostly, two families of errors that can occur during an FTP session. The first kind are the network related issues like a connection reset, and they are usually fatal to the session, meaning, in all likelihood the connection to the server has been lost and the session should be restarted from scratch. These errors are reported by throwing an IOException. The second kind are the errors reported by the FTP server, like when trying to download a non-existing file for example. These errors are usually non fatal to the session, meaning more commands can be sent to the server. In these cases, a FtpProtocolException is thrown.

It should be noted that this is not a thread-safe API, as it wouldn't make too much sense, due to the very sequential nature of FTP, to provide a client able to be manipulated from multiple threads.

Since
1.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
private static final int

Constructor Summary

AccessConstructor and Description
protected
FtpClient()

Creates an instance of FtpClient.

Method Summary

Modifier and TypeMethod and Description
public abstract FtpClient

Returns:

this FtpClient
abort
()

Sends the ABOR command to the server.

public abstract FtpClient

Returns:

this FtpClient
allocate
(long
The number of bytes to allocate.
size
)

Sends the "Allocate" (ALLO) command to the server telling it to pre-allocate the specified number of bytes for the next transfer.

public abstract FtpClient

Returns:

this FtpClient
appendFile
(String
A String containing the name of the remote file to append to.
name
,
InputStream
The InputStream providing access to the data to be appended.
local
)

Sends the APPE command to the server in order to transfer a data stream passed in argument and append it to the content of the specified remote file.

public abstract FtpClient

Returns:

this FtpClient
changeDirectory
(String
path of the directory to CD to.
remoteDirectory
)

Changes to a specific directory on a remote FTP server

public abstract FtpClient

Returns:

this FtpClient
changeToParentDirectory
()

Changes to the parent directory, sending the CDUP command to the server.

public abstract void
close()

Redeclares java.io.Closeable.close.

Closes the current connection.

public abstract FtpClient

Returns:

this FtpClient
completePending
()

Some methods do not wait until completion before returning, so this method can be called to wait until completion.

public abstract FtpClient

Returns:

this FtpClient
connect
(SocketAddress
the address of the destination server
dest
)

Connects the FtpClient to the specified destination server.

public abstract FtpClient

Returns:

this FtpClient
connect
(SocketAddress
the address of the destination server
dest
,
int
the value, in milliseconds, to use as a connection timeout
timeout
)

Connects the FtpClient to the specified destination server.

public static FtpClient

Returns:

the created FtpClient
create
()

Creates an instance of FtpClient.

public static FtpClient

Returns:

The created FtpClient
create
(InetSocketAddress
the InetSocketAddress to connect to.
dest
)

Creates an instance of FtpClient and connects it to the specified address.

public static FtpClient

Returns:

The created FtpClient
create
(String
the String containing the name of the host to connect to.
dest
)

Creates an instance of FtpClient and connects it to the specified host on the default FTP port.

public static final int

Returns:

the port number.
defaultPort
()

Returns the default FTP port number.

public abstract FtpClient

Returns:

this FtpClient
deleteFile
(String
a String containing the name of the file to delete.
name
)

Deletes a file on the server.

public abstract FtpClient

Returns:

This FtpClient
enablePassiveMode
(boolean
true to force passive mode.
passive
)

Enables, or disables, the use of the passive mode.

public abstract FtpClient

Returns:

this FtpClient
endSecureSession
()

Sends a CCC command followed by a PROT C command to the server terminating an encrypted session and reverting back to a non encrypted transmission.

public abstract int

Returns:

the value, in milliseconds, of the current connect timeout.
getConnectTimeout
()

Returns the current default connection timeout value.

public abstract List<String>

Returns:

a List of Strings describing the supported additional features
getFeatures
()

Sends the FEAT command to the server and returns the list of supported features in the form of strings.

public abstract FtpClient

Returns:

this FtpClient
getFile
(String
a String containing the name of the file to retrieve from the server.
name
,
OutputStream
the OutputStream the file should be written to.
local
)

Retrieves a file from the ftp server and writes its content to the specified OutputStream.

public abstract InputStream

Returns:

the java.io.InputStream from the data connection
getFileStream
(String
the name of the remote file
name
)

Retrieves a file from the ftp server, using the RETR command, and returns the InputStream from the established data connection.

public abstract String

Returns:

a String containing the text sent back by the server, or null if the command failed.
getHelp
(String
the command for which the help is requested or null for the general help
cmd
)

Sends the HELP command to the server, with an optional command, like SITE, and returns the text sent back by the server.

public abstract String

Returns:

the name the latest transferred file remote name, or null if that information is unavailable.
getLastFileName
()

Returns, when available, the remote name of the last transferred file.

public abstract Date

Returns:

a Date representing the last modification time or null if the server returned an error, which can be checked with getLastReplyCode().
getLastModified
(String
a String containing the pathname of the file.
path
)

Issues the MDTM [path] command to the server to get the modification time of a specific file on the server.

public abstract FtpReplyCode

Returns:

the lastReplyCode or null if none were received yet.
getLastReplyCode
()

Returns the last reply code sent by the server.

public abstract String

Returns:

the message string, which can be quite long, last returned by the server, or null if no response were received yet.
getLastResponseString
()

Returns the last response string sent by the server.

public abstract long

Returns:

the size of the latest transfer or -1 if either there was no transfer or the information was unavailable.
getLastTransferSize
()

Returns, when available, the size of the latest started transfer.

public abstract Proxy

Returns:

the Proxy, this client is using, or null if none is used.
getProxy
()

Get the proxy of this FtpClient

public abstract int

Returns:

the value, in milliseconds, of the current read timeout.
getReadTimeout
()

Returns the current read timeout value.

public abstract SocketAddress

Returns:

the SocketAddress of the server, or null if this client is not connected yet.
getServerAddress
()

Retrieves the address of the FTP server this client is connected to.

public abstract long

Returns:

a long containing the size of the file or -1 if the server returned an error, which can be checked with getLastReplyCode().
getSize
(String
a String containing the pathname of the file.
path
)

Issues the SIZE [path] command to the server to get the size of a specific file on the server.

public abstract String

Returns:

the response from the server
getStatus
(String
an optional String containing the pathname the STAT command should apply to.
name
)

Sends the STAT command to the server.

public abstract String

Returns:

a String describing the OS, or null if the operation was not successful.
getSystem
()

Sends a System (SYST) command to the server and returns the String sent back by the server describing the operating system at the server.

public abstract String

Returns:

a String containing the message the server returned during connection or null.
getWelcomeMsg
()

Returns the Welcome string the server sent during initial connection.

public abstract String

Returns:

a String containing the current working directory
getWorkingDirectory
()

Retrieve the server current working directory using the PWD command.

public abstract boolean

Returns:

true if the client is connected.
isConnected
()

Tests whether this client is connected or not to a server.

public abstract boolean

Returns:

true if the client has already completed a login.
isLoggedIn
()

Checks whether the client is logged in to the server or not.

public abstract boolean

Returns:

true if the passive mode has been enabled.
isPassiveModeEnabled
()

Tests whether passive mode is enabled.

public abstract InputStream

Returns:

the InputStream from the resulting data connection
list
(String
the pathname of the directory to list, or null for the current working directory.
path
)

Issues a LIST command to the server to get the current directory listing, and returns the InputStream from the data connection.

public abstract Iterator<FtpDirEntry>

Returns:

an Iterator of files or null if the command failed.
listFiles
(String
the pathname of the directory to list or null for the current working directory.
path
)

Issues a MLSD command to the server to get the specified directory listing and applies the internal parser to create an Iterator of java.net.FtpDirEntry.

public abstract FtpClient

Returns:

this FtpClient
login
(String
The user name
user
,
char[]
The password for that user
password
)

Attempts to log on the server with the specified user name and password.

public abstract FtpClient

Returns:

this FtpClient
login
(String
The user name
user
,
char[]
The password for that user.
password
,
String
The account name for that user.
account
)

Attempts to log on the server with the specified user name, password and account name.

public abstract FtpClient

Returns:

this FtpClient
makeDirectory
(String
a String containing the name of the directory to create.
name
)

Creates a new directory on the server.

public abstract InputStream

Returns:

the InputStream from the resulting data connection
nameList
(String
a String containing the pathname of the directory to list or null for the current working directory.
path
)

Issues a NLST path command to server to get the specified directory content.

public abstract FtpClient

Returns:

this FtpClient
noop
()

Sends a No-operation command.

public FtpClient

Returns:

this FtpClient
putFile
(String
the name of the remote file to write.
name
,
InputStream
the InputStream that points to the data to transfer.
local
)

Transfers a file from the client to the server (aka a put) by sending the STOR or STOU command, depending on the unique argument.

public abstract FtpClient

Returns:

this FtpClient
putFile
(String
the name of the remote file to write.
name
,
InputStream
the InputStream that points to the data to transfer.
local
,
boolean
true if the remote file should be unique (i.e. not already existing), false otherwise.
unique
)

Transfers a file from the client to the server (aka a put) by sending the STOR command.

public OutputStream

Returns:

the java.io.OutputStream from the data connection or null if the command was unsuccessful.
putFileStream
(String
the name of the remote file to write.
name
)

Transfers a file from the client to the server (aka a put) by sending the STOR command, and returns the OutputStream from the established data connection.

public abstract OutputStream

Returns:

the java.io.OutputStream from the data connection.
putFileStream
(String
the name of the remote file to write.
name
,
boolean
true if the remote files should be unique, in which case the STOU command will be used.
unique
)

Transfers a file from the client to the server (aka a put) by sending the STOR or STOU command, depending on the unique argument, and returns the OutputStream from the established data connection.

public abstract FtpClient

Returns:

this FtpClient
reInit
()

Reinitializes the USER parameters on the FTP server

public abstract FtpClient

Returns:

this FtpClient
removeDirectory
(String
a String containing the name of the directory to remove.
name
)

Removes a directory on the server.

public abstract FtpClient

Returns:

this FtpClient
rename
(String
the name of the file being renamed
from
,
String
the new name for the file
to
)

Renames a file on the server.

public FtpClient

Returns:

This FtpClient
setAsciiType
()

Changes the current transfer type to ascii.

public FtpClient

Returns:

This FtpClient
setBinaryType
()

Changes the current transfer type to binary.

public abstract FtpClient

Returns:

This FtpClient
setConnectTimeout
(int
the timeout value, in milliseconds, to use for the connect operation. A value of zero or less, means use the default timeout.
timeout
)

Sets the default timeout value to use when connecting to the server,

public abstract FtpClient

Returns:

this FtpClient
setDirParser
(FtpDirParser
The FtpDirParser to use.
p
)

Sets the parser used to handle the directory output to the specified one.

public abstract FtpClient

Returns:

This FtpClient
setProxy
(Proxy
the Proxy to use, or null for no proxy.
p
)

Set the Proxy to be used for the next connection.

public abstract FtpClient

Returns:

This FtpClient
setReadTimeout
(int
the timeout value, in milliseconds, to use for the read operation. A value of zero or less, means use the default timeout.
timeout
)

Sets the timeout value to use when reading from the server,

public abstract FtpClient

Returns:

this FtpClient
setRestartOffset
(long
the offset in the remote file at which to start the next transfer. This must be a value greater than or equal to zero.
offset
)

Sets the restart offset to the specified value.

public abstract FtpClient

Returns:

This FtpClient
setType
(FtpClient.TransferType
the TransferType to use.
type
)

Changes the transfer type (binary, ascii, ebcdic) and issue the proper command (e.g. TYPE A) to the server.

public abstract FtpClient

Returns:

this FtpClient
siteCmd
(String
the command to be sent.
cmd
)

Sends the SITE command to the server.

public abstract FtpClient

Returns:

this FtpCLient
startSecureSession
()

Attempts to switch to a secure, encrypted connection.

public abstract FtpClient

Returns:

this FtpClient
structureMount
(String
a String containing the name of the structure to mount.
struct
)

Sends the "Structure Mount" (SMNT) command to the server.

public abstract FtpClient

Returns:

this FtpClient
useKerberos
()

Attempts to use Kerberos GSSAPI as an authentication mechanism with the ftp server.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait