|
Java™ Platform Standard Ed. 7 DRAFT internal-b00 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.net.FtpClient
public class FtpClient
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 functionality 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();
if (cl.connect("ftp.gnu.org") &&
cl.login("anonymous", "john.doe@mydomain.com".toCharArray())) {
cl.changeDirectory("pub/gnu");
Iterator<FtpFile> dir = cl.listFiles();
while (dir.hasNext()) {
FtpFile f = dir.next();
System.err.println(f.getName());
}
cl.logout();
}
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.
| Nested Class Summary | |
|---|---|
static class |
FtpClient.TransferMode
|
static class |
FtpClient.TransferType
|
| Constructor Summary | |
|---|---|
protected |
FtpClient()
Creates an instance of FtpClient. |
| Method Summary | |
|---|---|
boolean |
abort()
sends the ABOR command to the server. |
boolean |
allocate(long size)
Sends the "Allocate" (ALLO) command to the server telling it to pre-allocate the specified number of bytes for the next transfer. |
boolean |
appendFile(String name,
InputStream 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. |
boolean |
changeDirectory(String remoteDirectory)
Changes to a specific directory on a remote FTP server |
boolean |
changeToParentDirectory()
Changes to the parent directory, sending the CDUP command to the server. |
boolean |
clearTLS()
Sends a CCC command followed by a PROT C
command to the server terminating an encrypted session and reverting
back to a non crypted transmission. |
boolean |
completePending()
Some methods do not wait until completion before returning, so this method can be called to wait until completion. |
void |
connect(InetSocketAddress dest)
Connects the FtpClient to the specified destination. |
void |
connect(String dest)
Connects the FtpClient to the specified destination server using the default FTP port. |
void |
connect(String dest,
int port)
Connects the FtpClient to the specified destination server using the specified port. |
static FtpClient |
create()
Creates an instance of FtpClient. |
static FtpClient |
create(InetSocketAddress dest)
Creates an instance of FtpClient and connects it to the specified address. |
static FtpClient |
create(String dest)
Creates an instance of FtpClient and connects it to the
specified host on the default FTP port. |
static int |
defaultPort()
Returns the default FTP port number. |
boolean |
deleteFile(String name)
Deletes a file on the server. |
int |
getConnectTimeout()
Returns the current connection timeout value. |
List<String> |
getFeatures()
Sends the FEAT command to the server and returns the list of supported features in the form of strings. |
boolean |
getFile(String name,
OutputStream local)
Retrieves a file from the ftp server and writes it to the specified OutputStream. |
InputStream |
getFileStream(String name)
Retrieves a file from the ftp server, using the RETR command, and returns the InputStream from* the established data connection. |
String |
getHelp(String cmd)
Sends the HELP command to the server, with an optional command, like SITE, and returns the text sent back by the server. |
String |
getLastFileName()
Returns, when available, the remote name of the last transfered file. |
Date |
getLastModified(String path)
Issues the MDTM [path] command to the server to get the modification time of a specific file on the server. |
FtpReplyCode |
getLastReplyCode()
Returns the last reply code sent by the server. |
String |
getLastResponseString()
Returns the last response string sent by the server. |
long |
getLastTransferSize()
Returns, when available, the size of the latest started transfer. |
Proxy |
getProxy()
Get the proxy of this FtpClient |
int |
getReadTimeout()
Returns the current read timeout value. |
long |
getSize(String path)
Issues the SIZE [path] command to the server to get the size of a specific file on the server. |
String |
getStatus(String name)
Sends the STAT command to the server. |
String |
getSystem()
Sends a SYST (System) command to the server and returns the String sent back by the server describing the operating system at the server. |
FtpClient.TransferMode |
getTransferMode()
Gets the current transfer mode. |
String |
getWelcomeMsg()
Returns the Welcome string the server sent during initial connection. |
String |
getWorkingDirectory()
Returns the server current working directory, or null if
the PWD command failed. |
boolean |
isConnected()
Tests whether this client is connected or not to a server. |
boolean |
isLoggedIn()
Checks whether the client is logged in to the server or not. |
InputStream |
list(String path)
Issues a LIST command to the server to get the current directory listing, and returns the InputStream from the data connection. |
Iterator<FtpFile> |
listFiles(String path)
Issues a MLSD command to the server to get the specified directory listing and applies the current parser to create an Iterator of java.net.ftp.FtpFile. |
boolean |
login(String user,
char[] password)
Attempts to log on the server with the specified user name and password. |
boolean |
login(String user,
char[] password,
String account)
Attempts to log on the server with the specified user name, password and account name. |
void |
logout()
Logs out the current user. |
boolean |
makeDirectory(String name)
Creates a new directory on the server. |
InputStream |
nameList(String path)
Issues a NLST path command to server to get the specified directory content. |
boolean |
noop()
Sends a No-operation command. |
boolean |
putFile(String name,
InputStream 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. |
boolean |
putFile(String name,
InputStream local,
boolean unique)
Transfers a file from the client to the server (aka a put) by sending the STOR command. |
OutputStream |
putFileStream(String 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. |
OutputStream |
putFileStream(String name,
boolean 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. |
void |
reInit()
Reinitializes the USER parameters on the FTP server |
boolean |
removeDirectory(String name)
Removes a directory on the server. |
boolean |
rename(String from,
String to)
Renames a file on the server. |
FtpClient |
setActiveMode()
Sets the transfer mode to active. |
FtpClient |
setAsciiType()
Changes the current transfer type to ascii. |
FtpClient |
setBinaryType()
Changes the current transfer type to binary. |
FtpClient |
setConnectTimeout(int timeout)
Sets the timeout value to use when connecting to the server, |
void |
setFileParser(FtpFileParser p)
Sets the parser used to handle the directory output to the specified one. |
FtpClient |
setPassiveMode()
Set the transfer mode to passive. |
FtpClient |
setProxy(Proxy p)
Set the Proxy to be used. |
FtpClient |
setReadTimeout(int timeout)
Sets the timeout value to use when reading from the server, |
void |
setRestartOffset(long offset)
Sets the restart offset to the specified value. |
FtpClient |
setType(FtpClient.TransferType type)
Changes the transfer type (binary, ascii, ebcdic) and issue the proper command (e.g. |
boolean |
siteCmd(String cmd)
Sends the SITE command to the server. |
boolean |
structureMount(String struct)
Sends the "Structure Mount" (SMNT) command to the server. |
boolean |
useGSSAPI()
Attempts to use Kerberos GSSAPI as an authentication mechanism with the ftp server. |
boolean |
useTLS()
Attempts to switch to a secure, encrypted connection. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
protected FtpClient()
| Method Detail |
|---|
public static int defaultPort()
public static FtpClient create()
public static FtpClient create(InetSocketAddress dest)
throws IOException
dest - the InetSocketAddress to connect to.
IOException - if the connection fails
public static FtpClient create(String dest)
throws IOException
FtpClient and connects it to the
specified host on the default FTP port.
dest - the String containing the name of the host
to connect to.
IOException - if the connection fails.public FtpClient setPassiveMode()
setActiveMode()public FtpClient setActiveMode()
setPassiveMode()public FtpClient.TransferMode getTransferMode()
TransferModepublic FtpClient setConnectTimeout(int timeout)
timeout - the timeout value, in milliseconds, to use for the connect
operation. A value of zero or less, means use the default timeout.
public int getConnectTimeout()
setConnectTimeout(int)public FtpClient setReadTimeout(int timeout)
timeout - the timeout value, in milliseconds, to use for the read
operation. A value of zero or less, means use the default timeout.
public int getReadTimeout()
setReadTimeout(int)
public FtpClient setProxy(Proxy p)
throws IOException
Proxy to be used.
p - the Proxy to use, or null for no proxy.
IOException - if the FtpClient is already connectedpublic Proxy getProxy()
Proxy, this client is using, or null
if none is used.setProxy(Proxy)public boolean isConnected()
true if the client is connected.
public void connect(String dest)
throws IOException
dest - the name of the destination server
IOException - if connection failed.
public void connect(String dest,
int port)
throws IOException
dest - the name of the destination server.port - the port number of the destination server.
IOException - if connection failed.
public void connect(InetSocketAddress dest)
throws IOException
dest - the address of the destination server
IOException - if connection failed.
public boolean login(String user,
char[] password)
throws IOException
user - The user namepassword - The password for that user
true if the login was successful.
IOException - if an error occured during the transmission
public boolean login(String user,
char[] password,
String account)
throws IOException
user - The user namepassword - The password for that user.account - The account name for that user.
true if the login was successful.
IOException - if an error occurs during the transmission.public void logout()
public boolean isLoggedIn()
true if the client has already completed a login.
public boolean changeDirectory(String remoteDirectory)
throws IOException
remoteDirectory - path of the directory to CD to.
true if the operation was successful.
FtpProtocolException
IOException
public boolean changeToParentDirectory()
throws IOException
true if the command was successful.
IOException
public String getWorkingDirectory()
throws IOException
null if
the PWD command failed.
String containing the current working directory,
or null
IOExceptionpublic void setRestartOffset(long offset)
REST command to server before a file
transfer and has the effect of resuming a file transfer from the
specified point. After a transfer the restart offset is set back to
zero.
offset - the offset in the remote file at which to start the next
transfer. This must be a value greater than or equal to
zero.
IllegalArgumentException - if the offset is negative.
public boolean getFile(String name,
OutputStream local)
throws IOException
OutputStream.
If the restart offset was set, then a REST command will be
sent before the RETR in order to restart the tranfer from the specified
offset.
The OutputStream is not closed by this method at the end
of the transfer.
name - a String containing the name of the file to
retreive from the server.local - the OutputStream the file should be written to.
- Throws:
IOException - if the transfer fails.
public InputStream getFileStream(String name)
throws IOException
completePending() has to be called once the application
is done reading from the returned stream.
name - the name of the remote file
InputStream from the data connection, or
null if the command was unsuccessful.
IOException - if an error occured during the transmission.
public OutputStream putFileStream(String name)
throws IOException
OutputStream
from the established data connection.
A new file is created at the server site if the file specified does
not already exist.
completePending() has to be called once the application
is finished writing to the returned stream.
name - the name of the remote file to write.
OutputStream from the data connection or
null if the command was unsuccessful.
IOException - if an error occured during the transmission.
public OutputStream putFileStream(String name,
boolean unique)
throws IOException
unique argument, and returns the OutputStream
from the established data connection.
completePending() has to be called once the application
is finished writing to the stream.
A new file is created at the server site if the file specified does
not already exist.
If unique is set to true, the resultant file
is to be created under a name unique to that directory, meaning
it will not overwrite an existing file, instead the server will
generate a new, unique, file name.
The name of the remote file can be retrieved, after completion of the
transfer, by calling getLastFileName().
name - the name of the remote file to write.unique - true if the remote files should be unique,
in which case the STOU command will be used.
OutputStream from the data connection or
null if the command was unsuccessful.
IOException - if an error occured during the transmission.
public boolean putFile(String name,
InputStream local)
throws IOException
unique argument. The content of the InputStream
passed in argument is written into the remote file, overwriting any
existing data.
A new file is created at the server site if the file specified does
not already exist.
If unique is set to true, the resultant file
is to be created under a name unique to that directory, meaning
it will not overwrite an existing file, instead the server will
generate a new, unique, file name.
The name of the remote file can be retrieved, after completion of the
transfer, by calling getLastFileName().
name - the name of the remote file to write.local - the InputStream that points to the data to
transfer.
true if the transfer was successful.
IOException - if an error occured during the transmission.
public boolean putFile(String name,
InputStream local,
boolean unique)
throws IOException
InputStream
passed in argument is written into the remote file, overwriting any
existing data.
A new file is created at the server site if the file specified does
not already exist.
name - the name of the remote file to write.local - the InputStream that points to the data to
transfer.unique - true if the remote file should be unique
(i.e. not already existing), false otherwise.
true if the transfer was successful.
IOException - if an error occured during the transmission.getLastFileName()
public boolean appendFile(String name,
InputStream local)
throws IOException
name - A String containing the name of the remote file
to append to.local - The InputStream providing access to the data
to be appended.
true if the transfer was successful.
IOException - if an error occured during the transmission.
public boolean rename(String from,
String to)
throws IOException
from - the name of the file being renamedto - the new name for the file
IOException - if the command fails
public boolean deleteFile(String name)
throws IOException
name - a String containing the name of the file
to delete.
true if the command was successful
IOException - if an error occured during the exchange
public boolean makeDirectory(String name)
throws IOException
name - a String containing the name of the directory
to create.
true if the operation was successful.
IOException - if an error occured during the exchange
public boolean removeDirectory(String name)
throws IOException
name - a String containing the name of the directory
to remove.
true if the operation was successful.
IOException - if an error occured during the exchange.
public boolean noop()
throws IOException
FtpProtocolException - if the command fails
IOException
public String getStatus(String name)
throws IOException
null.
If used between file transfers, it may have a pathname as argument
in which case it will work as the LIST command except no data
connection will be created.
name - an optional String containing the pathname
the STAT command should apply to.
null if the
command failed.
IOException - if an error occured during the transmission.
public List<String> getFeatures()
throws IOException
null
List of Strings describing the
supported additional features, or null
if the command is not supported.
IOException - if an error occurs during the transmission.
public boolean abort()
throws IOException
true if the command was successful.
IOException - if an error occured during the transmission.
public boolean completePending()
throws IOException
getFileStream(String).
So this method should be called before accessing information related to
such a command.
This method will actually block reading on the command channel for a notification from the server that the command is finished. Such a notification often carries extra information concerning the completion of the pending action (e.g. number of bytes transfered).
Note that this will return true immediately if no command or action is pending
It should be also noted that most methods issuing commands to the ftp server will call this method if a previous command is pending.
Example of use:
InputStream in = cl.getFileStream("file");
...
cl.completePending();
long size = cl.getLastTransferSize();
On the other hand, it's not necessary in a case like:
InputStream in = cl.getFileStream("file");
// read content
...
cl.logout();
Since logout() will call completePending() if necessary.
true if the completion was successful or if no
action was pending.
IOException
public void reInit()
throws IOException
FtpProtocolException - if the command fails
IOException
public FtpClient setType(FtpClient.TransferType type)
throws IOException
type - the TransferType to use.
IOException - if an error occurs during transmission.
public FtpClient setBinaryType()
throws IOException
setType(TransferType.BINARY)
IOException - if an error occurs during the transmission.setType(TransferType)
public FtpClient setAsciiType()
throws IOException
setType(TransferType.ASCII)
IOException - if an error occurs during the transmission.setType(TransferType)
public InputStream list(String path)
throws IOException
completePending() has to be called once the application
is finished writing to the stream.
path - the pathname of the directory to list, or null
for the current working directory.
InputStream from the resulting data connection
IOException - if an error occurs during the transmission.changeDirectory(String),
listFiles(String)
public InputStream nameList(String path)
throws IOException
list(String) method by the fact that
it will only list the file names which would make the parsing of the
somewhat easier.
completePending() has to be called once the application
is finished writing to the stream.
path - a String containing the pathname of the
directory to list or null for the current working
directory.
InputStream from the resulting data connection
IOException - if an error occurs during the transmission.
public long getSize(String path)
throws IOException
path - a String containing the pathname of the
file.
long containing the size of the file or -1 if
the server returned an error, which can be checked with
getLastReplyCode().
IOException - if an error occurs during the transmission.
public Date getLastModified(String path)
throws IOException
null will be returned.
path - a String containing the pathname of the file.
Date representing the last modification time
or null if the server returned an error, which
can be checked with getLastReplyCode().
IOException - if an error occurs during the transmission.public void setFileParser(FtpFileParser p)
p - The FtpFileParser to use.listFiles(String)
public Iterator<FtpFile> listFiles(String path)
throws IOException
java.net.ftp.FtpFile. Note that the Iterator returned is also a
Closeable.
If the server doesn't support the MLSD command, the LIST command is used
instead.
completePending() has to be called once the application
is finished iterating through the files.
path - the pathname of the directory to list or null
for the current working directoty.
Iterator of files or null if the
command failed.
IOException - if an error occured during the transmissionsetFileParser(FtpFileParser),
changeDirectory(String)
public boolean useGSSAPI()
throws IOException
AUTH GSSAPI command, and if
it is accepted by the server, will followup with ADAT
command to exchange the various tokens until authentification is
successful. This conforms to Appendix I of RFC 2228.
true if authentication was successful.
IOException - if an error occurs during the transmission.public String getWelcomeMsg()
String containing the message the server
returned during connection or null.public FtpReplyCode getLastReplyCode()
public String getLastResponseString()
public long getLastTransferSize()
public String getLastFileName()
null if that information is unavailable.
public boolean useTLS()
throws IOException
See RFC 4217
If successful this will establish a secure command channel with the server, it will also make it so that all other transfers (e.g. a RETR command) will be done over an encrypted channel as well unless areInit() command or a clearTLS() command is issued.
true if the operation was successful.
IOException - if an error occured during the transmission.clearTLS()
public boolean clearTLS()
throws IOException
CCC command followed by a PROT C
command to the server terminating an encrypted session and reverting
back to a non crypted transmission.
true if the operation was successful.
IOException - if an error occured during transmission.
public boolean allocate(long size)
throws IOException
size - The number of bytes to allocate.
true if the operation was successful.
IOException - if an error occured during the transmission.
public boolean structureMount(String struct)
throws IOException
struct - a String containing the name of the
structure to mount.
true if the operation was successful.
IOException - if an error occured during the transmission.
public String getSystem()
throws IOException
String describing the OS, or null
if the operation was not successful.
IOException - if an error occured during the transmission.
public String getHelp(String cmd)
throws IOException
cmd - the command for which the help is requested or
null for the general help
String containing the text sent back by the
server, or null if the command failed.
IOException - if an error occured during transmission
public boolean siteCmd(String cmd)
throws IOException
cmd - the command to be sent.
true if the command was successful.
IOException - if an error occured during transmission
|
Java™ Platform Standard Ed. 7 DRAFT internal-b00 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Use is subject to license terms.