Class FTPClient
try { int reply; ftp.connect("ftp.foobar.com"); System.out.println("Connected to " + server + "."); System.out.print(ftp.getReplyString()); // After connection attempt, you should check the reply code to verify // success. reply = ftp.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } } catch(IOException e) { if(ftp.isConnected()) { try { ftp.disconnect(); } catch(IOException f) { // do nothing } } System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); }
Immediately after connecting is the only real time you need to check the reply code (because connect is of type void). The convention for all the FTP command methods in FTPClient is such that they either return a boolean value or some other value. The boolean methods return true on a successful completion reply from the FTP server and false on a reply resulting in an error condition or failure. The methods returning a value other than boolean return a value containing the higher level data produced by the FTP command, or null if a reply resulted in an error condition or failure. If you want to access the exact FTP reply code causing a success or failure, you must call getReplyCode after a success or failure.
The default settings for FTPClient are for it to use
FTP.ASCII_FILE_TYPE
,
FTP.NON_PRINT_TEXT_FORMAT
,
FTP.STREAM_TRANSFER_MODE
, and
FTP.FILE_STRUCTURE
. The only file types directly supported
are FTP.ASCII_FILE_TYPE
and
FTP.IMAGE_FILE_TYPE
(which is the same as
FTP.BINARY_FILE_TYPE
). Because there are at lest 4
different EBCDIC encodings, we have opted not to provide direct support
for EBCDIC. To transfer EBCDIC and other unsupported file types you
must create your own filter InputStreams and OutputStreams and wrap
them around the streams returned or required by the FTPClient methods.
FTPClient uses the NetASCII filter streams in
com.oroinc.io to provide
transparent handling of ASCII files. We will consider incorporating
EBCDIC support if there is enough demand.
FTP.NON_PRINT_TEXT_FORMAT
,
FTP.STREAM_TRANSFER_MODE
, and
FTP.FILE_STRUCTURE
are the only supported formats,
transfer modes, and file structures.
Because the handling of sockets on different platforms can differ significantly, the FTPClient automatically issues a new PORT command prior to every transfer requiring that the server connect to the client's data port. This ensures identical problem-free behavior on Windows, Unix, and Macintosh platforms. Additionally, it relieves programmers from having to issue the PORT command themselves and dealing with platform dependent issues.
Additionally, for security purposes, all data connections to the client are verified to ensure that they originated from the intended party (host and port). If a data connection is initiated by an unexpected party, the command will close the socket and throw an IOException. You may disable this behavior with setRemoteVerificationEnabled().
You should keep in mind that the FTP server may choose to prematurely
close a connection if the client has been idle for longer than a
given time period (usually 900 seconds). The FTPClient class will detect a
premature FTP server connection closing when it receives a
FTPReply.SERVICE_NOT_AVAILABLE response to a command.
When that occurs, the FTP class method encountering that reply will throw
an
FTPConnectionClosedException .
FTPConnectionClosedException
is a subclass of IOException
and therefore need not be
caught separately, but if you are going to catch it separately, its
catch block must appear before the more general IOException
catch block. When you encounter an
FTPConnectionClosedException , you must disconnect the connection with
disconnect() to properly clean up the
system resources used by FTPClient. Before disconnecting, you may check the
last reply code and text with
getReplyCode ,
getReplyString ,
and
getReplyStrings.
You may avoid server disconnections while the client is idle by
periodicaly sending NOOP commands to the server.
Rather than list it separately for each method, we mention here that every method communicating with the server and throwing an IOException can also throw a MalformedServerReplyException , which is a subclass of IOException. A MalformedServerReplyException will be thrown when the reply received from the server deviates enough from the protocol specification that it cannot be interpreted in a useful manner despite attempts to be as lenient as possible.
- Author:
- Daniel F. Savarese
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
A constant indicating the FTP session is expecting all transfers to occur between the client (local) and server and that the server should connect to the client's data port to initiate a data transfer.static final int
A constant indicating the FTP session is expecting all transfers to occur between two remote servers and that the server the client is connected to should connect to the other server's data port to initiate a data transfer.static final int
A constant indicating the FTP session is expecting all transfers to occur between the client (local) and server and that the server is in passive mode, requiring the client to connect to the server's data port to initiate a transfer.static final int
A constant indicating the FTP session is expecting all transfers to occur between two remote servers and that the server the client is connected to is in passive mode, requiring the other server to connect to the first server's data port to initiate a data transfer.Fields inherited from class com.oroinc.net.ftp.FTP
_commandSupport_, ASCII_FILE_TYPE, BINARY_FILE_TYPE, BLOCK_TRANSFER_MODE, CARRIAGE_CONTROL_TEXT_FORMAT, COMPRESSED_TRANSFER_MODE, DEFAULT_DATA_PORT, DEFAULT_PORT, EBCDIC_FILE_TYPE, FILE_STRUCTURE, IMAGE_FILE_TYPE, LOCAL_FILE_TYPE, NON_PRINT_TEXT_FORMAT, PAGE_STRUCTURE, RECORD_STRUCTURE, STREAM_TRANSFER_MODE, TELNET_TEXT_FORMAT
Fields inherited from class com.oroinc.net.SocketClient
_defaultPort_, _input_, _isConnected_, _output_, _socket_, _socketFactory_, _timeout_, NETASCII_EOL
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Handles special connection requirements.boolean
abort()
Abort a transfer in progress.boolean
allocate
(int bytes) Reserve a number of bytes on the server for the next file transfer.boolean
allocate
(int bytes, int recordSize) Reserve space on the server for the next file transfer.boolean
appendFile
(String remote, InputStream local) Appends to a file on the server with the given name, taking input from the given InputStream.appendFileStream
(String remote) Returns an OutputStream through which data can be written to append to a file on the server with the given name.boolean
Change to the parent directory of the current working directory.boolean
changeWorkingDirectory
(String pathname) Change the current working directory of the FTP session.boolean
There are a few FTPClient methods that do not complete the entire sequence of FTP commands to complete a transaction.boolean
deleteFile
(String pathname) Deletes a file on the FTP server.void
Closes the connection to the FTP server and restores connection parameters to the default values.void
Set the current data connection mode toACTIVE_LOCAL_DATA_CONNECTION_MODE
.void
Set the current data connection mode toPASSIVE_LOCAL_DATA_CONNECTION_MODE
.boolean
enterRemoteActiveMode
(InetAddress host, int port) Set the current data connection mode toACTIVE_REMOTE_DATA_CONNECTION
.boolean
Set the current data connection mode toPASSIVE_REMOTE_DATA_CONNECTION_MODE
.int
Returns the current data connection mode (one of the_DATA_CONNECTION_MODE
constants.Returns the hostname or IP address (in the form of a string) returned by the server when entering passive mode.int
If in passive mode, returns the data port of the passive host.Issue the FTP STAT command to the server.Issue the FTP STAT command to the server for a given pathname.Fetches the system type name from the server and returns the string.boolean
Return whether or not verification of the remote host participating in data connections is enabled.FTPFile[]
Using theDefaultFTPFileListParser
, obtain a list of file information for the current working directory.FTPFile[]
listFiles
(FTPFileListParser parser) Using a programmer specifiedFTPFileListParser
, obtain a list of file information for the current working directory.FTPFile[]
listFiles
(FTPFileListParser parser, String pathname) Using a programmer specifiedFTPFileListParser
, obtain a list of file information for a directory or information for just a single file.FTPFile[]
Using theDefaultFTPFileListParser
, obtain a list of file information for a directory or information for just a single file.listHelp()
Fetches the system help information from the server and returns the full string.Fetches the help information for a given command from the server and returns the full string.String[]
Obtain a list of filenames in the current working directory This information is obtained through the NLST command.String[]
Obtain a list of filenames in a directory (or just the name of a given file, which is not particularly useful).boolean
Login to the FTP server using the provided username and password.boolean
Login to the FTP server using the provided username, password, and account.boolean
logout()
Logout of the FTP server by sending the QUIT command.boolean
makeDirectory
(String pathname) Creates a new subdirectory on the FTP server in the current directory (if a relative pathname is given) or where specified (if an absolute pathname is given).Returns the pathname of the current working directory.boolean
remoteAppend
(String filename) Initiate a server to server file transfer.boolean
remoteRetrieve
(String filename) Initiate a server to server file transfer.boolean
remoteStore
(String filename) Initiate a server to server file transfer.boolean
Initiate a server to server file transfer.boolean
remoteStoreUnique
(String filename) Initiate a server to server file transfer.boolean
removeDirectory
(String pathname) Removes a directory on the FTP server (if empty).boolean
Renames a remote file.boolean
restart
(long offset) Restart aSTREAM_TRANSFER_MODE
file transfer starting from the given offset.boolean
retrieveFile
(String remote, OutputStream local) Retrieves a named file from the server and writes it to the given OutputStream.retrieveFileStream
(String remote) Returns an InputStream from which a named file from the server can be read.boolean
sendNoOp()
Sends a NOOP command to the FTP server.boolean
sendSiteCommand
(String arguments) Send a site specific command.void
setDataTimeout
(int timeout) Sets the timeout in milliseconds to use when reading from the data connection.boolean
setFileStructure
(int structure) Sets the file structure.boolean
setFileTransferMode
(int mode) Sets the transfer mode.boolean
setFileType
(int fileType) Sets the file type to be transferred.boolean
setFileType
(int fileType, int formatOrByteSize) Sets the file type to be transferred and the format.void
setRemoteVerificationEnabled
(boolean enable) Enable or disable verification that the remote host taking part of a data connection is the same as the host to which the control connection is attached.boolean
storeFile
(String remote, InputStream local) Stores a file on the server using the given name and taking input from the given InputStream.storeFileStream
(String remote) Returns an OutputStream through which data can be written to store a file on the server using the given name.boolean
storeUniqueFile
(InputStream local) Stores a file on the server using a unique name assigned by the server and taking input from the given InputStream.boolean
storeUniqueFile
(String remote, InputStream local) Stores a file on the server using a unique name derived from the given name and taking input from the given InputStream.Returns an OutputStream through which data can be written to store a file on the server using a unique name assigned by the server.storeUniqueFileStream
(String remote) Returns an OutputStream through which data can be written to store a file on the server using a unique name derived from the given name.boolean
structureMount
(String pathname) Issue the FTP SMNT command.Methods inherited from class com.oroinc.net.ftp.FTP
abor, acct, addProtocolCommandListener, allo, allo, appe, cdup, cwd, dele, getReply, getReplyCode, getReplyString, getReplyStrings, help, help, list, list, mkd, mode, nlst, nlst, noop, pass, pasv, port, pwd, quit, rein, removeProtocolCommandistener, rest, retr, rmd, rnfr, rnto, sendCommand, sendCommand, sendCommand, sendCommand, site, smnt, stat, stat, stor, stou, stou, stru, syst, type, type, user
Methods inherited from class com.oroinc.net.telnet.TelnetClient
getInputStream, getOutputStream
Methods inherited from class com.oroinc.net.SocketClient
connect, connect, connect, connect, connect, connect, getDefaultPort, getDefaultTimeout, getLocalAddress, getLocalPort, getRemoteAddress, getRemotePort, getSoLinger, getSoTimeout, getTcpNoDelay, isConnected, setDefaultPort, setDefaultTimeout, setSocketFactory, setSoLinger, setSoTimeout, setTcpNoDelay, verifyRemote
-
Field Details
-
ACTIVE_LOCAL_DATA_CONNECTION_MODE
public static final int ACTIVE_LOCAL_DATA_CONNECTION_MODEA constant indicating the FTP session is expecting all transfers to occur between the client (local) and server and that the server should connect to the client's data port to initiate a data transfer. This is the default data connection mode when and FTPClient instance is created.- See Also:
-
ACTIVE_REMOTE_DATA_CONNECTION_MODE
public static final int ACTIVE_REMOTE_DATA_CONNECTION_MODEA constant indicating the FTP session is expecting all transfers to occur between two remote servers and that the server the client is connected to should connect to the other server's data port to initiate a data transfer.- See Also:
-
PASSIVE_LOCAL_DATA_CONNECTION_MODE
public static final int PASSIVE_LOCAL_DATA_CONNECTION_MODEA constant indicating the FTP session is expecting all transfers to occur between the client (local) and server and that the server is in passive mode, requiring the client to connect to the server's data port to initiate a transfer.- See Also:
-
PASSIVE_REMOTE_DATA_CONNECTION_MODE
public static final int PASSIVE_REMOTE_DATA_CONNECTION_MODEA constant indicating the FTP session is expecting all transfers to occur between two remote servers and that the server the client is connected to is in passive mode, requiring the other server to connect to the first server's data port to initiate a data transfer.- See Also:
-
-
Constructor Details
-
FTPClient
public FTPClient()Default FTPClient constructor. Creates a new FTPClient instance with the data connection mode set toACTIVE_LOCAL_DATA_CONNECTION_MODE
, the file type set toFTP.ASCII_FILE_TYPE
, the file format set toFTP.NON_PRINT_TEXT_FORMAT
, the file structure set toFTP.FILE_STRUCTURE
, and the transfer mode set toFTP.STREAM_TRANSFER_MODE
.
-
-
Method Details
-
_connectAction_
Description copied from class:TelnetClient
Handles special connection requirements.- Overrides:
_connectAction_
in classFTP
- Throws:
IOException
- If an error occurs during connection setup.
-
setDataTimeout
public void setDataTimeout(int timeout) Sets the timeout in milliseconds to use when reading from the data connection. This timeout will be set immediately after opening the data connection.- Parameters:
timeout
- The default timeout in milliseconds that is used when opening a data connection socket.
-
disconnect
Closes the connection to the FTP server and restores connection parameters to the default values.- Overrides:
disconnect
in classFTP
- Throws:
IOException
- If an error occurs while disconnecting.
-
setRemoteVerificationEnabled
public void setRemoteVerificationEnabled(boolean enable) Enable or disable verification that the remote host taking part of a data connection is the same as the host to which the control connection is attached. The default is for verification to be enabled. You may set this value at any time, whether the FTPClient is currently connected or not.- Parameters:
enable
- True to enable verification, false to disable verification.
-
isRemoteVerificationEnabled
public boolean isRemoteVerificationEnabled()Return whether or not verification of the remote host participating in data connections is enabled. The default behavior is for verification to be enabled.- Returns:
- True if verification is enabled, false if not.
-
login
Login to the FTP server using the provided username and password.- Parameters:
username
- The username to login under.password
- The password to use.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
login
Login to the FTP server using the provided username, password, and account. If no account is required by the server, only the username and password, the account information is not used.- Parameters:
username
- The username to login under.password
- The password to use.account
- The account to use.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
logout
Logout of the FTP server by sending the QUIT command.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
changeWorkingDirectory
Change the current working directory of the FTP session.- Parameters:
pathname
- The new current working directory.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
changeToParentDirectory
Change to the parent directory of the current working directory.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
structureMount
Issue the FTP SMNT command.- Parameters:
pathname
- The pathname to mount.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
enterLocalActiveMode
public void enterLocalActiveMode()Set the current data connection mode toACTIVE_LOCAL_DATA_CONNECTION_MODE
. No communication with the FTP server is conducted, but this causes all future data transfers to require the FTP server to connect to the client's data port. Additionally, to accommodate differences between socket implementations on different platforms, this method causes the client to issue a PORT command before every data transfer. -
enterLocalPassiveMode
public void enterLocalPassiveMode()Set the current data connection mode toPASSIVE_LOCAL_DATA_CONNECTION_MODE
. Use this method only for data transfers between the client and server. This method causes a PASV command to be issued to the server before the opening of every data connection, telling the server to open a data port to which the client will connect to conduct data transfers. The FTPClient will stay inPASSIVE_LOCAL_DATA_CONNECTION_MODE
until the mode is changed by calling some other method such as enterLocalActiveMode() -
enterRemoteActiveMode
Set the current data connection mode toACTIVE_REMOTE_DATA_CONNECTION
. Use this method only for server to server data transfers. This method issues a PORT command to the server, indicating the other server and port to which it should connect for data transfers. You must call this method before EVERY server to server transfer attempt. The FTPClient will NOT automatically continue to issue PORT commands. You also must remember to call enterLocalActiveMode() if you wish to return to the normal data connection mode.- Parameters:
host
- The passive mode server accepting connections for data transfers.port
- The passive mode server's data port.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
enterRemotePassiveMode
Set the current data connection mode toPASSIVE_REMOTE_DATA_CONNECTION_MODE
. Use this method only for server to server data transfers. This method issues a PASV command to the server, telling it to open a data port to which the active server will connect to conduct data transfers. You must call this method before EVERY server to server transfer attempt. The FTPClient will NOT automatically continue to issue PASV commands. You also must remember to call enterLocalActiveMode() if you wish to return to the normal data connection mode.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
getPassiveHost
Returns the hostname or IP address (in the form of a string) returned by the server when entering passive mode. If not in passive mode, returns null. This method only returns a valid value AFTER a data connection has been opened after a call to enterLocalPassiveMode(). This is because FTPClient sends a PASV command to the server only just before opening a data connection, and not when you call enterLocalPassiveMode().- Returns:
- The passive host name if in passive mode, otherwise null.
-
getPassivePort
public int getPassivePort()If in passive mode, returns the data port of the passive host. This method only returns a valid value AFTER a data connection has been opened after a call to enterLocalPassiveMode(). This is because FTPClient sends a PASV command to the server only just before opening a data connection, and not when you call enterLocalPassiveMode().- Returns:
- The data port of the passive server. If not in passive mode, undefined.
-
getDataConnectionMode
public int getDataConnectionMode()Returns the current data connection mode (one of the_DATA_CONNECTION_MODE
constants.- Returns:
- The current data connection mode (one of the
_DATA_CONNECTION_MODE
constants.
-
setFileType
Sets the file type to be transferred. This should be one ofFTP.ASCII_FILE_TYPE
,FTP.IMAGE_FILE_TYPE
, etc. The file type only needs to be set when you want to change the type. After changing it, the new type stays in effect until you change it again. The default file type isFTP.ASCII_FILE_TYPE
if this method is never called.- Parameters:
fileType
- The_FILE_TYPE
constant indcating the type of file.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
setFileType
Sets the file type to be transferred and the format. The type should be one ofFTP.ASCII_FILE_TYPE
,FTP.IMAGE_FILE_TYPE
, etc. The file type only needs to be set when you want to change the type. After changing it, the new type stays in effect until you change it again. The default file type isFTP.ASCII_FILE_TYPE
if this method is never called. The format should be one of the FTP classTEXT_FORMAT
constants, or if the type isFTP.LOCAL_FILE_TYPE
, the format should be the byte size for that type. The default format isFTP.NON_PRINT_TEXT_FORMAT
if this method is never called.- Parameters:
fileType
- The_FILE_TYPE
constant indcating the type of file.formatOrByteSize
- The format of the file (one of the_FORMAT
constants. In the case ofLOCAL_FILE_TYPE
, the byte size.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
setFileStructure
Sets the file structure. The default structure isFTP.FILE_STRUCTURE
if this method is never called.- Parameters:
structure
- The structure of the file (one of the FTP class_STRUCTURE
constants).- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
setFileTransferMode
Sets the transfer mode. The default transfer modeFTP.STREAM_TRANSFER_MODE
if this method is never called.- Parameters:
mode
- The new transfer mode to use (one of the FTP class_TRANSFER_MODE
constants).- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
remoteRetrieve
Initiate a server to server file transfer. This method tells the server to which the client is connected to retrieve a given file from the other server.- Parameters:
filename
- The name of the file to retrieve.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
remoteStore
Initiate a server to server file transfer. This method tells the server to which the client is connected to store a file on the other server using the given filename. The other server must have had aremoteRetrieve
issued to it by another FTPClient.- Parameters:
filename
- The name to call the file that is to be stored.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
remoteStoreUnique
Initiate a server to server file transfer. This method tells the server to which the client is connected to store a file on the other server using a unique filename based on the given filename. The other server must have had aremoteRetrieve
issued to it by another FTPClient.- Parameters:
filename
- The name on which to base the filename of the file that is to be stored.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
remoteStoreUnique
Initiate a server to server file transfer. This method tells the server to which the client is connected to store a file on the other server using a unique filename. The other server must have had aremoteRetrieve
issued to it by another FTPClient. Many FTP servers require that a base filename be given from which the unique filename can be derived. For those servers use the other version ofremoteStoreUnique
- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
remoteAppend
Initiate a server to server file transfer. This method tells the server to which the client is connected to append to a given file on the other server. The other server must have had aremoteRetrieve
issued to it by another FTPClient.- Parameters:
filename
- The name of the file to be appended to, or if the file does not exist, the name to call the file being stored.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
completePendingCommand
There are a few FTPClient methods that do not complete the entire sequence of FTP commands to complete a transaction. These commands require some action by the programmer after the reception of a positive intermediate command. After the programmer's code completes its actions, it must call this method to receive the completion reply from the server and verify the success of the entire transaction.For example,
InputStream input; OutputStream output; input = new FileInputStream("foobaz.txt"); output = ftp.storeFileStream("foobar.txt") if(!FTPReply.isPositiveIntermediate(ftp.getReplyCode())) { input.close(); output.close(); ftp.logout(); ftp.disconnect(); System.err.println("File transfer failed."); System.exit(1); } Util.copyStream(input, output); input.close(); output.close(); // Must call completePendingCommand() to finish command. if(!ftp.completePendingCommand()) { ftp.logout(); ftp.disconnect(); System.err.println("File transfer failed."); System.exit(1); }
- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
retrieveFile
Retrieves a named file from the server and writes it to the given OutputStream. This method does NOT close the given OutputStream. If the current file type is ASCII, line separators in the file are converted to the local representation.- Parameters:
remote
- The name of the remote file.local
- The local OutputStream to which to write the file.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.CopyStreamException
- If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to determine the number of bytes transferred and the IOException causing the error. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
retrieveFileStream
Returns an InputStream from which a named file from the server can be read. If the current file type is ASCII, the returned InputStream will convert line separators in the file to the local representation. You must close the InputStream when you finish reading from it. The InputStream itself will take care of closing the parent data connection socket upon being closed. To finalize the file transfer you must call completePendingCommand and check its return value to verify success.- Parameters:
remote
- The name of the remote file.- Returns:
- An InputStream from which the remote file can be read. If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
storeFile
Stores a file on the server using the given name and taking input from the given InputStream. This method does NOT close the given InputStream. If the current file type is ASCII, line separators in the file are transparently converted to the NETASCII format (i.e., you should not attempt to create a special InputStream to do this).- Parameters:
remote
- The name to give the remote file.local
- The local InputStream from which to read the file.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.CopyStreamException
- If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to determine the number of bytes transferred and the IOException causing the error. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
storeFileStream
Returns an OutputStream through which data can be written to store a file on the server using the given name. If the current file type is ASCII, the returned OutputStream will convert line separators in the file to the NETASCII format (i.e., you should not attempt to create a special OutputStream to do this). You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the parent data connection socket upon being closed. To finalize the file transfer you must call completePendingCommand and check its return value to verify success.- Parameters:
remote
- The name to give the remote file.- Returns:
- An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
appendFile
Appends to a file on the server with the given name, taking input from the given InputStream. This method does NOT close the given InputStream. If the current file type is ASCII, line separators in the file are transparently converted to the NETASCII format (i.e., you should not attempt to create a special InputStream to do this).- Parameters:
remote
- The name of the remote file.local
- The local InputStream from which to read the data to be appended to the remote file.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.CopyStreamException
- If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to determine the number of bytes transferred and the IOException causing the error. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
appendFileStream
Returns an OutputStream through which data can be written to append to a file on the server with the given name. If the current file type is ASCII, the returned OutputStream will convert line separators in the file to the NETASCII format (i.e., you should not attempt to create a special OutputStream to do this). You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the parent data connection socket upon being closed. To finalize the file transfer you must call completePendingCommand and check its return value to verify success.- Parameters:
remote
- The name of the remote file.- Returns:
- An OutputStream through which the remote file can be appended. If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
storeUniqueFile
Stores a file on the server using a unique name derived from the given name and taking input from the given InputStream. This method does NOT close the given InputStream. If the current file type is ASCII, line separators in the file are transparently converted to the NETASCII format (i.e., you should not attempt to create a special InputStream to do this).- Parameters:
remote
- The name on which to base the unique name given to the remote file.local
- The local InputStream from which to read the file.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.CopyStreamException
- If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to determine the number of bytes transferred and the IOException causing the error. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
storeUniqueFileStream
Returns an OutputStream through which data can be written to store a file on the server using a unique name derived from the given name. If the current file type is ASCII, the returned OutputStream will convert line separators in the file to the NETASCII format (i.e., you should not attempt to create a special OutputStream to do this). You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the parent data connection socket upon being closed. To finalize the file transfer you must call completePendingCommand and check its return value to verify success.- Parameters:
remote
- The name on which to base the unique name given to the remote file.- Returns:
- An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
storeUniqueFile
Stores a file on the server using a unique name assigned by the server and taking input from the given InputStream. This method does NOT close the given InputStream. If the current file type is ASCII, line separators in the file are transparently converted to the NETASCII format (i.e., you should not attempt to create a special InputStream to do this).- Parameters:
local
- The local InputStream from which to read the file.remote
- The name to give the remote file.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.CopyStreamException
- If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to determine the number of bytes transferred and the IOException causing the error. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
storeUniqueFileStream
Returns an OutputStream through which data can be written to store a file on the server using a unique name assigned by the server. If the current file type is ASCII, the returned OutputStream will convert line separators in the file to the NETASCII format (i.e., you should not attempt to create a special OutputStream to do this). You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the parent data connection socket upon being closed. To finalize the file transfer you must call completePendingCommand and check its return value to verify success.- Parameters:
remote
- The name to give the remote file.- Returns:
- An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
allocate
Reserve a number of bytes on the server for the next file transfer.- Parameters:
bytes
- The number of bytes which the server should allocate.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
allocate
Reserve space on the server for the next file transfer.- Parameters:
bytes
- The number of bytes which the server should allocate.bytes
- The size of a file record.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
restart
Restart aSTREAM_TRANSFER_MODE
file transfer starting from the given offset. This will only work on FTP servers supporting the REST comand for the stream transfer mode. However, most FTP servers support this. Any subsequent file transfer will start reading or writing the remote file from the indicated offset.- Parameters:
offset
- The offset into the remote file at which to start the next file transfer.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
rename
Renames a remote file.- Parameters:
from
- The name of the remote file to rename.to
- The new name of the remote file.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
abort
Abort a transfer in progress.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
deleteFile
Deletes a file on the FTP server.- Parameters:
pathname
- The pathname of the file to be deleted.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
removeDirectory
Removes a directory on the FTP server (if empty).- Parameters:
pathname
- The pathname of the directory to remove.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
makeDirectory
Creates a new subdirectory on the FTP server in the current directory (if a relative pathname is given) or where specified (if an absolute pathname is given).- Parameters:
pathname
- The pathname of the directory to create.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
printWorkingDirectory
Returns the pathname of the current working directory.- Returns:
- The pathname of the current working directory. If it cannot be obtained, returns null.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
sendSiteCommand
Send a site specific command.- Parameters:
argument
- The site specific command and arguments.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
getSystemName
Fetches the system type name from the server and returns the string.- Returns:
- The system type name obtained from the server. null if the information could not be obtained.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
listHelp
Fetches the system help information from the server and returns the full string.- Returns:
- The system help string obtained from the server. null if the information could not be obtained.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
listHelp
Fetches the help information for a given command from the server and returns the full string.- Parameters:
The
- command on which to ask for help.- Returns:
- The command help string obtained from the server. null if the information could not be obtained.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
sendNoOp
Sends a NOOP command to the FTP server. This is useful for preventing server timeouts.- Returns:
- True if successfully completed, false if not.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
listNames
Obtain a list of filenames in a directory (or just the name of a given file, which is not particularly useful). This information is obtained through the NLST command. If the given pathname is a directory and contains no files, a zero length array is returned only if the FTP server returned a positive completion code, otherwise null is returned (the FTP server returned a 550 error No files found.). If the directory is not empty, an array of filenames in the directory is returned. If the pathname corresponds to a file, only that file will be listed. The server may or may not expand glob expressions.- Parameters:
pathname
- The file or directory to list.- Returns:
- The list of filenames contained in the given path. null if the list could not be obtained. If there are no filenames in the directory, a zero-length array is returned.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
listNames
Obtain a list of filenames in the current working directory This information is obtained through the NLST command. If the current directory contains no files, a zero length array is returned only if the FTP server returned a positive completion code, otherwise, null is returned (the FTP server returned a 550 error No files found.). If the directory is not empty, an array of filenames in the directory is returned.- Returns:
- The list of filenames contained in the current working directory. null if the list could not be obtained. If there are no filenames in the directory, a zero-length array is returned.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
listFiles
Using a programmer specifiedFTPFileListParser
, obtain a list of file information for a directory or information for just a single file. This information is obtained through the LIST command. The contents of the returned array is determined by theFTPFileListParser
used. The server may or may not expand glob expressions. You should avoid using glob expressions because the return format for glob listings differs from server to server and will likely cause this method to fail.- Parameters:
parser
- TheFTPFileListParser
that should be used to parse the server file listing.pathname
- The file or directory to list.- Returns:
- The list of file information contained in the given path in
the format determined by the
parser
parameter. - Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
listFiles
Using a programmer specifiedFTPFileListParser
, obtain a list of file information for the current working directory. This information is obtained through the LIST command. The contents of the array returned is determined by theFTPFileListParser
used.- Parameters:
parser
- TheFTPFileListParser
that should be used to parse the server file listing.- Returns:
- The list of file information contained in the given path in
the format determined by the
parser
parameter. - Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
listFiles
Using theDefaultFTPFileListParser
, obtain a list of file information for a directory or information for just a single file. This information is obtained through the LIST command. If the given pathname is a directory and contains no files,null
is returned, otherwise an array ofFTPFile
instances representing the files in the directory is returned. If the pathname corresponds to a file, only the information for that file will be contained in the array (which will be of length 1). The server may or may not expand glob expressions. You should avoid using glob expressions because the return format for glob listings differs from server to server and will likely cause this method to fail.- Parameters:
pathname
- The file or directory to list.- Returns:
- The list of file information contained in the given path. null if the list could not be obtained or if there are no files in the directory.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
listFiles
Using theDefaultFTPFileListParser
, obtain a list of file information for the current working directory. This information is obtained through the LIST command. If the given current directory contains no files null is returned, otherwise an array ofFTPFile
instances representing the files in the directory is returned.- Returns:
- The list of file information contained in the current working directory. null if the list could not be obtained or if there are no files in the directory.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
getStatus
Issue the FTP STAT command to the server.- Returns:
- The status information returned by the server.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-
getStatus
Issue the FTP STAT command to the server for a given pathname. This should produce a listing of the file or directory.- Returns:
- The status information returned by the server.
- Throws:
FTPConnectionClosedException
- If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.IOException
- If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
-