gnu.inet.ftp
Class FTPConnection

java.lang.Object
  extended by gnu.inet.ftp.FTPConnection

public class FTPConnection
extends java.lang.Object

An FTP client connection, or PI. This implements RFC 959, with the following exceptions:

Author:
Chris Burdess

Field Summary
protected static java.lang.String ABOR
           
protected static java.lang.String ACCT
           
protected static java.lang.String ALLO
           
protected static java.lang.String APPE
           
protected static java.lang.String AUTH
           
protected static java.lang.String CCC
           
protected static java.lang.String CDUP
           
protected  int connectionTimeout
          The timeout when attempting to connect a socket.
protected static java.lang.String CWD
           
protected  boolean debug
          If true, print debugging information.
protected static java.lang.String DELE
           
protected  gnu.inet.ftp.DTP dtp
          The current data transfer process in use by this connection.
protected  int fileStructure
          The current file structure type.
static int FTP_DATA_PORT
          The FTP data port.
static int FTP_PORT
          The default FTP transmission control port.
protected static java.lang.String HELP
           
protected  LineInputStream in
          The socket input stream.
protected static java.lang.String LIST
           
protected static java.lang.String MKD
           
protected static java.lang.String MODE
           
static int MODE_BLOCK
           
static int MODE_COMPRESSED
           
static int MODE_STREAM
           
protected static java.lang.String NLST
           
protected static java.lang.String NOOP
           
protected  CRLFOutputStream out
          The socket output stream.
protected static java.lang.String PASS
           
protected  boolean passive
          If true, use passive mode.
protected static java.lang.String PASV
           
protected static java.lang.String PBSZ
           
protected static java.lang.String PORT
           
protected static java.lang.String PROT
           
protected static java.lang.String PWD
           
protected static java.lang.String QUIT
           
protected static java.lang.String REIN
           
protected  int representationType
          The current representation type.
protected static java.lang.String REST
           
protected static java.lang.String RETR
           
protected static java.lang.String RMD
           
protected static java.lang.String RNFR
           
protected static java.lang.String RNTO
           
protected static java.lang.String SITE
           
protected static java.lang.String SMNT
           
protected  java.net.Socket socket
          The socket used to communicate with the server.
protected static java.lang.String STAT
           
protected static java.lang.String STOR
           
protected static java.lang.String STOU
           
protected static java.lang.String STRU
           
static int STRUCTURE_FILE
           
static int STRUCTURE_PAGE
           
static int STRUCTURE_RECORD
           
protected static java.lang.String SYST
           
protected  int timeout
          The read timeout on sockets.
protected static java.lang.String TLS
           
protected  int transferMode
          The current transfer mode.
protected static java.lang.String TYPE
           
static int TYPE_ASCII
           
static int TYPE_BINARY
           
static int TYPE_EBCDIC
           
protected static java.lang.String USER
           
 
Constructor Summary
FTPConnection(java.lang.String hostname)
          Creates a new connection to the server using the default port.
FTPConnection(java.lang.String hostname, int port)
          Creates a new connection to the server.
FTPConnection(java.lang.String hostname, int port, int connectionTimeout, int timeout, boolean debug)
          Creates a new connection to the server.
 
Method Summary
 boolean abort()
          Aborts the transfer in progress.
 void allocate(long size)
          This command may be required by some servers to reserve sufficient storage to accommodate the new file to be transferred.
 java.io.OutputStream append(java.lang.String filename)
          Returns a stream for uploading a file.
 boolean authenticate(java.lang.String username, java.lang.String password)
          Authenticate using the specified username and password.
 boolean changeToParentDirectory()
          Changes directory to the parent of the current working directory.
 boolean changeWorkingDirectory(java.lang.String path)
          Changes directory to the specified path.
 boolean delete(java.lang.String filename)
          Causes the file specified to be deleted at the server site.
 int getFileStructure()
          Returns the current file structure type.
 int getRepresentationType()
          Returns the current representation type of the transfer data.
protected  FTPResponse getResponse()
          Reads the next response from the server.
 int getTransferMode()
          Returns the current transfer mode.
 java.lang.String getWorkingDirectory()
          Returns the current working directory.
protected  void initialiseDTP()
          Initialise the data transfer process.
 java.io.InputStream list(java.lang.String pathname)
          Returns a listing of information about the specified pathname.
 void logout()
          Terminates the control connection.
 boolean makeDirectory(java.lang.String pathname)
          Causes the directory specified to be created at the server site.
 java.util.List nameList(java.lang.String pathname)
          Returns a directory listing.
 void noop()
          Does nothing.
protected  FTPResponse readResponse()
          Reads and parses the next response from the server.
 void reinitialize()
          Terminates an authenticated login.
 boolean removeDirectory(java.lang.String pathname)
          Causes the directory specified to be deleted.
 boolean rename(java.lang.String oldName, java.lang.String newName)
          Renames a file.
 java.io.InputStream retrieve(java.lang.String filename)
          Retrieves the specified file.
protected  void send(java.lang.String cmd)
          Sends the specified command line to the server.
 void setFileStructure(int structure)
          Sets the desired file structure type.
 void setPassive(boolean flag)
          Set passive mode.
 void setRepresentationType(int type)
          Sets the desired representation type of the transfer data.
 void setTransferMode(int mode)
          Sets the desired transfer mode.
 boolean starttls(boolean confidential)
          Negotiates TLS over the current connection.
 boolean starttls(boolean confidential, javax.net.ssl.TrustManager tm)
          Negotiates TLS over the current connection.
 java.io.OutputStream store(java.lang.String filename)
          Returns a stream for uploading a file.
 java.lang.String system()
          Returns the type of operating system at the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FTP_PORT

public static final int FTP_PORT
The default FTP transmission control port.

See Also:
Constant Field Values

FTP_DATA_PORT

public static final int FTP_DATA_PORT
The FTP data port.

See Also:
Constant Field Values

USER

protected static final java.lang.String USER
See Also:
Constant Field Values

PASS

protected static final java.lang.String PASS
See Also:
Constant Field Values

ACCT

protected static final java.lang.String ACCT
See Also:
Constant Field Values

CWD

protected static final java.lang.String CWD
See Also:
Constant Field Values

CDUP

protected static final java.lang.String CDUP
See Also:
Constant Field Values

SMNT

protected static final java.lang.String SMNT
See Also:
Constant Field Values

REIN

protected static final java.lang.String REIN
See Also:
Constant Field Values

QUIT

protected static final java.lang.String QUIT
See Also:
Constant Field Values

PORT

protected static final java.lang.String PORT
See Also:
Constant Field Values

PASV

protected static final java.lang.String PASV
See Also:
Constant Field Values

TYPE

protected static final java.lang.String TYPE
See Also:
Constant Field Values

STRU

protected static final java.lang.String STRU
See Also:
Constant Field Values

MODE

protected static final java.lang.String MODE
See Also:
Constant Field Values

RETR

protected static final java.lang.String RETR
See Also:
Constant Field Values

STOR

protected static final java.lang.String STOR
See Also:
Constant Field Values

STOU

protected static final java.lang.String STOU
See Also:
Constant Field Values

APPE

protected static final java.lang.String APPE
See Also:
Constant Field Values

ALLO

protected static final java.lang.String ALLO
See Also:
Constant Field Values

REST

protected static final java.lang.String REST
See Also:
Constant Field Values

RNFR

protected static final java.lang.String RNFR
See Also:
Constant Field Values

RNTO

protected static final java.lang.String RNTO
See Also:
Constant Field Values

ABOR

protected static final java.lang.String ABOR
See Also:
Constant Field Values

DELE

protected static final java.lang.String DELE
See Also:
Constant Field Values

RMD

protected static final java.lang.String RMD
See Also:
Constant Field Values

MKD

protected static final java.lang.String MKD
See Also:
Constant Field Values

PWD

protected static final java.lang.String PWD
See Also:
Constant Field Values

LIST

protected static final java.lang.String LIST
See Also:
Constant Field Values

NLST

protected static final java.lang.String NLST
See Also:
Constant Field Values

SITE

protected static final java.lang.String SITE
See Also:
Constant Field Values

SYST

protected static final java.lang.String SYST
See Also:
Constant Field Values

STAT

protected static final java.lang.String STAT
See Also:
Constant Field Values

HELP

protected static final java.lang.String HELP
See Also:
Constant Field Values

NOOP

protected static final java.lang.String NOOP
See Also:
Constant Field Values

AUTH

protected static final java.lang.String AUTH
See Also:
Constant Field Values

PBSZ

protected static final java.lang.String PBSZ
See Also:
Constant Field Values

PROT

protected static final java.lang.String PROT
See Also:
Constant Field Values

CCC

protected static final java.lang.String CCC
See Also:
Constant Field Values

TLS

protected static final java.lang.String TLS
See Also:
Constant Field Values

TYPE_ASCII

public static final int TYPE_ASCII
See Also:
Constant Field Values

TYPE_EBCDIC

public static final int TYPE_EBCDIC
See Also:
Constant Field Values

TYPE_BINARY

public static final int TYPE_BINARY
See Also:
Constant Field Values

STRUCTURE_FILE

public static final int STRUCTURE_FILE
See Also:
Constant Field Values

STRUCTURE_RECORD

public static final int STRUCTURE_RECORD
See Also:
Constant Field Values

STRUCTURE_PAGE

public static final int STRUCTURE_PAGE
See Also:
Constant Field Values

MODE_STREAM

public static final int MODE_STREAM
See Also:
Constant Field Values

MODE_BLOCK

public static final int MODE_BLOCK
See Also:
Constant Field Values

MODE_COMPRESSED

public static final int MODE_COMPRESSED
See Also:
Constant Field Values

socket

protected java.net.Socket socket
The socket used to communicate with the server.


in

protected LineInputStream in
The socket input stream.


out

protected CRLFOutputStream out
The socket output stream.


connectionTimeout

protected int connectionTimeout
The timeout when attempting to connect a socket.


timeout

protected int timeout
The read timeout on sockets.


debug

protected boolean debug
If true, print debugging information.


dtp

protected gnu.inet.ftp.DTP dtp
The current data transfer process in use by this connection.


representationType

protected int representationType
The current representation type.


fileStructure

protected int fileStructure
The current file structure type.


transferMode

protected int transferMode
The current transfer mode.


passive

protected boolean passive
If true, use passive mode.

Constructor Detail

FTPConnection

public FTPConnection(java.lang.String hostname)
              throws java.net.UnknownHostException,
                     java.io.IOException
Creates a new connection to the server using the default port.

Parameters:
hostname - the hostname of the server to connect to
Throws:
java.net.UnknownHostException
java.io.IOException

FTPConnection

public FTPConnection(java.lang.String hostname,
                     int port)
              throws java.net.UnknownHostException,
                     java.io.IOException
Creates a new connection to the server.

Parameters:
hostname - the hostname of the server to connect to
port - the port to connect to(if <=0, use default port)
Throws:
java.net.UnknownHostException
java.io.IOException

FTPConnection

public FTPConnection(java.lang.String hostname,
                     int port,
                     int connectionTimeout,
                     int timeout,
                     boolean debug)
              throws java.net.UnknownHostException,
                     java.io.IOException
Creates a new connection to the server.

Parameters:
hostname - the hostname of the server to connect to
port - the port to connect to(if <=0, use default port)
connectionTimeout - the connection timeout, in milliseconds
timeout - the I/O timeout, in milliseconds
debug - print debugging information
Throws:
java.net.UnknownHostException
java.io.IOException
Method Detail

authenticate

public boolean authenticate(java.lang.String username,
                            java.lang.String password)
                     throws java.io.IOException
Authenticate using the specified username and password. If the username suffices for the server, the password will not be used and may be null.

Parameters:
username - the username
password - the optional password
Returns:
true on success, false otherwise
Throws:
java.io.IOException

starttls

public boolean starttls(boolean confidential)
                 throws java.io.IOException
Negotiates TLS over the current connection. See IETF draft-murray-auth-ftp-ssl-15.txt for details.

Parameters:
confidential - whether to provide confidentiality for the connection
Throws:
java.io.IOException

starttls

public boolean starttls(boolean confidential,
                        javax.net.ssl.TrustManager tm)
                 throws java.io.IOException
Negotiates TLS over the current connection. See IETF draft-murray-auth-ftp-ssl-15.txt for details.

Parameters:
confidential - whether to provide confidentiality for the connection
tm - the trust manager used to validate the server certificate.
Throws:
java.io.IOException

changeWorkingDirectory

public boolean changeWorkingDirectory(java.lang.String path)
                               throws java.io.IOException
Changes directory to the specified path.

Parameters:
path - an absolute or relative pathname
Returns:
true on success, false if the specified path does not exist
Throws:
java.io.IOException

changeToParentDirectory

public boolean changeToParentDirectory()
                                throws java.io.IOException
Changes directory to the parent of the current working directory.

Returns:
true on success, false otherwise
Throws:
java.io.IOException

reinitialize

public void reinitialize()
                  throws java.io.IOException
Terminates an authenticated login. If file transfer is in progress, it remains active for result response only.

Throws:
java.io.IOException

logout

public void logout()
            throws java.io.IOException
Terminates the control connection. The file transfer connection remains open for result response only. This connection is invalid and no further commands may be issued.

Throws:
java.io.IOException

initialiseDTP

protected void initialiseDTP()
                      throws java.io.IOException
Initialise the data transfer process.

Throws:
java.io.IOException

setPassive

public void setPassive(boolean flag)
                throws java.io.IOException
Set passive mode.

Parameters:
flag - true if we should use passive mode, false otherwise
Throws:
java.io.IOException

getRepresentationType

public int getRepresentationType()
Returns the current representation type of the transfer data.

Returns:
TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY

setRepresentationType

public void setRepresentationType(int type)
                           throws java.io.IOException
Sets the desired representation type of the transfer data.

Parameters:
type - TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY
Throws:
java.io.IOException

getFileStructure

public int getFileStructure()
Returns the current file structure type.

Returns:
STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE

setFileStructure

public void setFileStructure(int structure)
                      throws java.io.IOException
Sets the desired file structure type.

Parameters:
structure - STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE
Throws:
java.io.IOException

getTransferMode

public int getTransferMode()
Returns the current transfer mode.

Returns:
MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED

setTransferMode

public void setTransferMode(int mode)
                     throws java.io.IOException
Sets the desired transfer mode.

Parameters:
mode - MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED
Throws:
java.io.IOException

retrieve

public java.io.InputStream retrieve(java.lang.String filename)
                             throws java.io.IOException
Retrieves the specified file.

Parameters:
filename - the filename of the file to retrieve
Returns:
an InputStream containing the file content
Throws:
java.io.IOException

store

public java.io.OutputStream store(java.lang.String filename)
                           throws java.io.IOException
Returns a stream for uploading a file. If a file with the same filename already exists on the server, it will be overwritten.

Parameters:
filename - the name of the file to save the content as
Returns:
an OutputStream to write the file data to
Throws:
java.io.IOException

append

public java.io.OutputStream append(java.lang.String filename)
                            throws java.io.IOException
Returns a stream for uploading a file. If a file with the same filename already exists on the server, the content specified will be appended to the existing file.

Parameters:
filename - the name of the file to save the content as
Returns:
an OutputStream to write the file data to
Throws:
java.io.IOException

allocate

public void allocate(long size)
              throws java.io.IOException
This command may be required by some servers to reserve sufficient storage to accommodate the new file to be transferred. It should be immediately followed by a store or append.

Parameters:
size - the number of bytes of storage to allocate
Throws:
java.io.IOException

rename

public boolean rename(java.lang.String oldName,
                      java.lang.String newName)
               throws java.io.IOException
Renames a file.

Parameters:
oldName - the current name of the file
newName - the new name
Returns:
true if successful, false otherwise
Throws:
java.io.IOException

abort

public boolean abort()
              throws java.io.IOException
Aborts the transfer in progress.

Returns:
true if a transfer was in progress, false otherwise
Throws:
java.io.IOException

delete

public boolean delete(java.lang.String filename)
               throws java.io.IOException
Causes the file specified to be deleted at the server site.

Parameters:
filename - the file to delete
Throws:
java.io.IOException

removeDirectory

public boolean removeDirectory(java.lang.String pathname)
                        throws java.io.IOException
Causes the directory specified to be deleted. This may be an absolute or relative pathname.

Parameters:
pathname - the directory to delete
Throws:
java.io.IOException

makeDirectory

public boolean makeDirectory(java.lang.String pathname)
                      throws java.io.IOException
Causes the directory specified to be created at the server site. This may be an absolute or relative pathname.

Parameters:
pathname - the directory to create
Throws:
java.io.IOException

getWorkingDirectory

public java.lang.String getWorkingDirectory()
                                     throws java.io.IOException
Returns the current working directory.

Throws:
java.io.IOException

list

public java.io.InputStream list(java.lang.String pathname)
                         throws java.io.IOException
Returns a listing of information about the specified pathname. If the pathname specifies a directory or other group of files, the server should transfer a list of files in the specified directory. If the pathname specifies a file then the server should send current information on the file. A null argument implies the user's current working or default directory.

Parameters:
pathname - the context pathname, or null
Throws:
java.io.IOException

nameList

public java.util.List nameList(java.lang.String pathname)
                        throws java.io.IOException
Returns a directory listing. The pathname should specify a directory or other system-specific file group descriptor; a null argument implies the user's current working or default directory.

Parameters:
pathname - the directory pathname, or null
Returns:
a list of filenames(strings)
Throws:
java.io.IOException

system

public java.lang.String system()
                        throws java.io.IOException
Returns the type of operating system at the server.

Throws:
java.io.IOException

noop

public void noop()
          throws java.io.IOException
Does nothing. This method can be used to ensure that the connection does not time out.

Throws:
java.io.IOException

send

protected void send(java.lang.String cmd)
             throws java.io.IOException
Sends the specified command line to the server. The CRLF sequence is automatically appended.

Parameters:
cmd - the command line to send
Throws:
java.io.IOException

getResponse

protected FTPResponse getResponse()
                           throws java.io.IOException
Reads the next response from the server. If the server sends the "transfer complete" code, this is handled here, and the next response is passed to the caller.

Throws:
java.io.IOException

readResponse

protected FTPResponse readResponse()
                            throws java.io.IOException
Reads and parses the next response from the server.

Throws:
java.io.IOException


© Copyright 2003 The Free Software Foundation, all rights reserved