public class

FtpClient

extends TransferProtocolClient
java.lang.Object
   ↳ sun.net.NetworkClient
     ↳ sun.net.TransferProtocolClient
       ↳ sun.net.ftp.FtpClient

Class Overview

This class implements the FTP client.

Summary

Constants
int FTP_PORT
Fields
public String welcomeMsg Welcome message from the server, if any.
[Expand]
Inherited Fields
From class sun.net.TransferProtocolClient
From class sun.net.NetworkClient
Public Constructors
FtpClient(String host)
New FTP client connected to host host.
FtpClient(String host, int port)
New FTP client connected to host host, port port.
FtpClient()
Create an uninitialized FTP client.
FtpClient(Proxy p)
Public Methods
TelnetOutputStream append(String filename)
Append to a file on the FTP server
void ascii()
Set transfer type to 'A'
void binary()
Set transfer type to 'I'
void cd(String remoteDirectory)
CD to a specific directory on a remote FTP server
void cdUp()
CD to the parent directory on a remote FTP server
void closeServer()
issue the QUIT command to the FTP server and close the connection.
TelnetInputStream get(String filename)
GET a file from the FTP server
static String getFtpProxyHost()
static int getFtpProxyPort()
static boolean getUseFtpProxy()
TelnetInputStream list()
LIST files in the current directory on a remote FTP server
void login(String user, String password)
login user to a host with username user and password password
static boolean matchNonProxyHosts(String host)
TelnetInputStream nameList(String path)
List (NLST) file names on a remote FTP server
void noop()
Send a No-operation command.
void openServer(String host)
Open a FTP connection to host host.
void openServer(String host, int port)
Open a FTP connection to host host on port port.
TelnetOutputStream put(String filename)
PUT a file to the FTP server
String pwd()
Print working directory of remote FTP server
void reInit()
Reinitialize the USER parameters on the FTp server
void rename(String from, String to)
Rename a file on the ftp server
String system()
Get the "System string" from the FTP server
Protected Methods
void finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
int issueCommand(String cmd)
Send a command to the FTP server.
void issueCommandCheck(String cmd)
Send a command to the FTP server and check for success.
Socket openDataConnection(String cmd)
Tries to open a Data Connection with the server.
Socket openPassiveDataConnection()
Tries to open a Data Connection in "PASSIVE" mode by issuing a EPSV or PASV command then opening a Socket to the specified address & port
int readReply()
Read the reply from the FTP server.
[Expand]
Inherited Methods
From class sun.net.TransferProtocolClient
From class sun.net.NetworkClient
From class java.lang.Object

Constants

public static final int FTP_PORT

Constant Value: 21 (0x00000015)

Fields

public String welcomeMsg

Welcome message from the server, if any.

Public Constructors

public FtpClient (String host)

New FTP client connected to host host.

Parameters
host Hostname of the FTP server
Throws
FtpProtocolException if the connection fails
IOException

public FtpClient (String host, int port)

New FTP client connected to host host, port port.

Parameters
host Hostname of the FTP server
port port number to connect to (usually 21)
Throws
FtpProtocolException if the connection fails
IOException

public FtpClient ()

Create an uninitialized FTP client.

public FtpClient (Proxy p)

Public Methods

public TelnetOutputStream append (String filename)

Append to a file on the FTP server

Parameters
filename name of the file to append to
Returns
  • the OutputStream to write the file to
Throws
IOException

public void ascii ()

Set transfer type to 'A'

Throws
FtpProtocolException if the command fails
IOException

public void binary ()

Set transfer type to 'I'

Throws
FtpProtocolException if the command fails
IOException

public void cd (String remoteDirectory)

CD to a specific directory on a remote FTP server

Parameters
remoteDirectory path of the directory to CD to
Throws
IOException

public void cdUp ()

CD to the parent directory on a remote FTP server

Throws
IOException

public void closeServer ()

issue the QUIT command to the FTP server and close the connection.

Throws
FtpProtocolException if an error occured
IOException

public TelnetInputStream get (String filename)

GET a file from the FTP server

Parameters
filename name of the file to retrieve
Returns
  • the InputStream to read the file from
Throws
FileNotFoundException if the file can't be opened
IOException

public static String getFtpProxyHost ()

Returns
  • the host to use, or null if none has been specified

public static int getFtpProxyPort ()

Returns
  • the proxy port to use. Will default reasonably if not set.

public static boolean getUseFtpProxy ()

Returns
  • if the networking layer should send ftp connections through a proxy

public TelnetInputStream list ()

LIST files in the current directory on a remote FTP server

Returns
  • the InputStream to read the list from
Throws
IOException

public void login (String user, String password)

login user to a host with username user and password password

Parameters
user Username to use at login
password Password to use at login or null of none is needed
Throws
FtpLoginException if login is unsuccesful
IOException

public static boolean matchNonProxyHosts (String host)

public TelnetInputStream nameList (String path)

List (NLST) file names on a remote FTP server

Parameters
path pathname to the directory to list, null for current directory
Returns
  • the InputStream to read the list from
Throws
IOException

public void noop ()

Send a No-operation command. It's usefull for testing the connection status

Throws
FtpProtocolException if the command fails
IOException

public void openServer (String host)

Open a FTP connection to host host.

Parameters
host The hostname of the ftp server
Throws
FtpProtocolException if connection fails
IOException

public void openServer (String host, int port)

Open a FTP connection to host host on port port.

Parameters
host the hostname of the ftp server
port the port to connect to (usually 21)
Throws
FtpProtocolException if connection fails
IOException

public TelnetOutputStream put (String filename)

PUT a file to the FTP server

Parameters
filename name of the file to store
Returns
  • the OutputStream to write the file to
Throws
IOException

public String pwd ()

Print working directory of remote FTP server

Throws
FtpProtocolException if the command fails
IOException

public void reInit ()

Reinitialize the USER parameters on the FTp server

Throws
FtpProtocolException if the command fails
IOException

public void rename (String from, String to)

Rename a file on the ftp server

Throws
FtpProtocolException if the command fails
IOException

public String system ()

Get the "System string" from the FTP server

Throws
FtpProtocolException if it fails
IOException

Protected Methods

protected void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
IOException

protected int issueCommand (String cmd)

Send a command to the FTP server.

Parameters
cmd String containing the command
Returns
  • reply code
Throws
FtpProtocolException if an error occured
IOException

protected void issueCommandCheck (String cmd)

Send a command to the FTP server and check for success.

Parameters
cmd String containing the command
Throws
FtpProtocolException if an error occured
IOException

protected Socket openDataConnection (String cmd)

Tries to open a Data Connection with the server. It will first try a passive mode connection, then, if it fails, a more traditional PORT command

Parameters
cmd the command to execute (RETR, STOR, etc...)
Returns
  • the opened socket
Throws
FtpProtocolException if an error occurs when issuing the PORT command to the ftp server.
IOException

protected Socket openPassiveDataConnection ()

Tries to open a Data Connection in "PASSIVE" mode by issuing a EPSV or PASV command then opening a Socket to the specified address & port

Returns
  • the opened socket
Throws
FtpProtocolException if an error occurs when issuing the PASV command to the ftp server.
IOException

protected int readReply ()

Read the reply from the FTP server.

Returns
  • FTP_SUCCESS or FTP_ERROR depending on success
Throws
FtpProtocolException if an error occured
IOException