Package com.enterprisedt.net.ftp
Class FileTransferClient
java.lang.Object
com.enterprisedt.net.ftp.FileTransferClient
- All Implemented Interfaces:
FileTransferClientInterface
Easy to use FTP client that is thread safe and provides true FTP streams.
This class is intended to replace FTPClient, which will eventually be
deprecated.
- Version:
- $Revision$
- Author:
- Bruce Blackshaw
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected com.enterprisedt.net.ftp.internal.EventAggregatorprotected EventListenerEvent listenersprotected com.enterprisedt.net.ftp.internal.ConnectionContextContext for the client that is the starting point for all new tasks. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCancel current transfer if underwayvoidchangeDirectory(String directoryName) Change directory on the FTP server.voidChange to parent directory on the FTP server.protected voidcheckConnection(boolean shouldBeConnected) Checks if the client has connected to the server and throws an exception if it hasn't.voidconnect()Make a connection to the FTP server.voidcreateDirectory(String directoryName) Create directory on the FTP server.voiddeleteDirectory(String directoryName) Delete directory on the FTP server.voiddeleteFile(String remoteFileName) Deletes a remote file.FTPFile[]List the current directory on the FTP server.FTPFile[]directoryList(String directoryName) List a directory on the FTP server.voiddirectoryList(String directoryName, DirectoryListCallback lister) List a directory on the FTP server.String[]List the names of files and directories in the current directory on the FTP server.String[]directoryNameList(String directoryName, boolean isLongListing) List the names of files and directories of a directory on the FTP server.voidDisconnect from the FTP server.voiddisconnect(boolean immediate) Disconnect from the FTP server.byte[]downloadByteArray(String remoteFileName) Download a file from the FTP server into a byte array.voiddownloadFile(String localFileName, String remoteFileName) Download a file from the FTP server .voiddownloadFile(String localFileName, String remoteFileName, WriteMode writeMode) Download a file from the FTP server .downloadStream(String remoteFileName) Download a file from the FTP server as a stream.static voiddownloadURLFile(String localFileName, String ftpURL) Downloads the given file to the given FTP URL.static FileTransferInputStreamdownloadURLStream(String ftpURL) Open an InputStream for the given FTP URL.executeCommand(String command) Request that the remote server execute the literal command supplied.booleanDetermine if a remote file exists.Get the advanced FTP configuration parameters objectGet the advanced general configuration parameters object, for none protocol specific parametersGet the current content type for all connections.Gets the last reply from the server, whether valid or notgetModifiedTime(String remoteFileName) Get the modified-time of a remote file.intGet the size of the network buffers (SO_SNDBUF and SO_RCVBUF).Get the current user password.Get the current remote directory.Returns the IP address or name of the remote host.intReturns the port being connected to on the remote server.longGet the size of a remote file.Get statistics on file transfers and deletions.Get a string that represents the remote system that the client is logged into.intReturns the timeout for socket connections.Get the current user name.booleanIs this client currently connected to the server?booleanGet the detect content type flagvoidPerform a manual login using the credentials that have been set.protected static voidprepareURLTransfer(URL url, FileTransferClientInterface client) voidRename a remote file or directory.voidSet the transfer type for all connections, either ASCII or binary.voidsetDetectContentType(boolean detectContentType) Set auto detect of filetypes on or off.voidsetEventListener(EventListener listener) Set the event listener for transfer event notificationvoidsetModifiedTime(String remoteFileName, Date modifiedTime) Set the modified-time of a remote file.voidsetNetworkBufferSize(int networkBufferSize) Set the size of the network buffers (SO_SNDBUF and SO_RCVBUF).voidsetPassword(String password) Set the password of the user to log in with.voidsetRemoteHost(String remoteHost) Set the IP address or name of the remote host This may only be done if the client is not already connected to the server.voidsetRemotePort(int remotePort) Set the port to connect to on the remote server.voidsetTimeout(int timeout) Set the timeout for socket connections.voidsetUserName(String userName) Set the name of the user to log in with.uploadFile(String localFileName, String remoteFileName) Upload a file to the FTP server.uploadFile(String localFileName, String remoteFileName, WriteMode writeMode) Upload a file to the FTP server.uploadStream(String remoteFileName) Upload a file to the FTP server by writing to a stream.uploadStream(String remoteFileName, WriteMode writeMode) Upload a file to the FTP server by writing to a stream.static voiduploadURLFile(String localFileName, String ftpURL) Uploads the given file to the given FTP URL.static FileTransferOutputStreamuploadURLStream(String ftpURL) Open an OutputStream for the given FTP URL.
-
Field Details
-
masterContext
protected com.enterprisedt.net.ftp.internal.ConnectionContext masterContextContext for the client that is the starting point for all new tasks. If we have a change directory task, when it is completed it will update this master context and the updated context will be used for all future tasks -
eventAggregator
protected com.enterprisedt.net.ftp.internal.EventAggregator eventAggregator -
listener
Event listeners
-
-
Constructor Details
-
FileTransferClient
public FileTransferClient()Default constructor
-
-
Method Details
-
checkConnection
Checks if the client has connected to the server and throws an exception if it hasn't. This is only intended to be used by subclasses- Throws:
FTPException- Thrown if the client has not connected to the server.
-
isConnected
public boolean isConnected()Is this client currently connected to the server?- Specified by:
isConnectedin interfaceFileTransferClientInterface- Returns:
- true if connected, false otherwise
-
getRemoteHost
Returns the IP address or name of the remote host.- Specified by:
getRemoteHostin interfaceFileTransferClientInterface- Returns:
- Returns the remote host.
-
setRemoteHost
Set the IP address or name of the remote host This may only be done if the client is not already connected to the server.- Specified by:
setRemoteHostin interfaceFileTransferClientInterface- Parameters:
remoteHost- The IP address or name of the remote host- Throws:
FTPException- Thrown if the client is already connected to the server.
-
getTimeout
public int getTimeout()Returns the timeout for socket connections.- Specified by:
getTimeoutin interfaceFileTransferClientInterface- Returns:
- Returns the connection timeout in milliseconds
-
setTimeout
Set the timeout for socket connections. Can only do this if not already connected.- Specified by:
setTimeoutin interfaceFileTransferClientInterface- Parameters:
timeout- the timeout to use in milliseconds- Throws:
FTPException- Thrown if the client is already connected to the server.
-
getNetworkBufferSize
public int getNetworkBufferSize()Get the size of the network buffers (SO_SNDBUF and SO_RCVBUF).- Specified by:
getNetworkBufferSizein interfaceFileTransferClientInterface- Returns:
- network buffer size
-
setNetworkBufferSize
Set the size of the network buffers (SO_SNDBUF and SO_RCVBUF).- Specified by:
setNetworkBufferSizein interfaceFileTransferClientInterface- Parameters:
networkBufferSize- new buffer size to set- Throws:
FTPException
-
getRemotePort
public int getRemotePort()Returns the port being connected to on the remote server.- Specified by:
getRemotePortin interfaceFileTransferClientInterface- Returns:
- Returns the port being connected to on the remote server.
-
setRemotePort
Set the port to connect to on the remote server. Can only do this if not already connected.- Specified by:
setRemotePortin interfaceFileTransferClientInterface- Parameters:
remotePort- The port to use.- Throws:
FTPException- Thrown if the client is already connected to the server.
-
setContentType
Set the transfer type for all connections, either ASCII or binary. Setting applies to all subsequent transfers that are initiated.- Specified by:
setContentTypein interfaceFileTransferClientInterface- Parameters:
type- transfer type- Throws:
FTPExceptionIOExceptionFTPException
-
getContentType
Get the current content type for all connections.- Specified by:
getContentTypein interfaceFileTransferClientInterface- Returns:
- transfer type
-
setDetectContentType
public void setDetectContentType(boolean detectContentType) Set auto detect of filetypes on or off. If on, the transfer mode is switched from ASCII to binary and vice versa depending on the extension of the file. After the transfer, the mode is always returned to what it was before the transfer was performed. The default is off. If the filetype is unknown, the transfer mode is unchanged- Specified by:
setDetectContentTypein interfaceFileTransferClientInterface- Parameters:
detectContentType- true if detecting content type, false if not
-
isDetectContentType
public boolean isDetectContentType()Get the detect content type flag- Specified by:
isDetectContentTypein interfaceFileTransferClientInterface- Returns:
- true if we are detecting binary and ASCII transfers from the file type
-
setUserName
Set the name of the user to log in with. Can only do this if not already connected.- Specified by:
setUserNamein interfaceFileTransferClientInterface- Parameters:
userName- user-name to log in with.- Throws:
FTPException
-
getPassword
Get the current user password.- Specified by:
getPasswordin interfaceFileTransferClientInterface- Returns:
- current user password
-
setPassword
Set the password of the user to log in with. Can only do this if not already connected.- Specified by:
setPasswordin interfaceFileTransferClientInterface- Parameters:
password- password to log in with.- Throws:
FTPException
-
getUserName
Get the current user name.- Specified by:
getUserNamein interfaceFileTransferClientInterface- Returns:
- current user name
-
getAdvancedFTPSettings
Get the advanced FTP configuration parameters object- Specified by:
getAdvancedFTPSettingsin interfaceFileTransferClientInterface- Returns:
- advanced parameters
-
getAdvancedSettings
Get the advanced general configuration parameters object, for none protocol specific parameters- Specified by:
getAdvancedSettingsin interfaceFileTransferClientInterface- Returns:
- advanced parameters
-
setEventListener
Set the event listener for transfer event notification- Specified by:
setEventListenerin interfaceFileTransferClientInterface- Parameters:
listener- event listener reference
-
connect
Make a connection to the FTP server.- Specified by:
connectin interfaceFileTransferClientInterface- Throws:
FTPExceptionIOException
-
manualLogin
Perform a manual login using the credentials that have been set. This should only be performed if auto login is disabled - normally connect() performs the login automatically.- Throws:
FTPExceptionIOException
-
prepareURLTransfer
protected static void prepareURLTransfer(URL url, FileTransferClientInterface client) throws IOException, FTPException - Throws:
IOExceptionFTPException
-
getStatistics
Get statistics on file transfers and deletions.- Specified by:
getStatisticsin interfaceFileTransferClientInterface- Returns:
- FTPStatistics
-
executeCommand
Request that the remote server execute the literal command supplied. In FTP, this is the equivalent of 'quote'. It could be used to send a SITE command to the server, e.g. "SITE recfm=FB lrecl=180 blksize=5400".- Specified by:
executeCommandin interfaceFileTransferClientInterface- Parameters:
command- command string- Returns:
- result string returned by server
- Throws:
FTPExceptionIOException
-
cancelAllTransfers
public void cancelAllTransfers()Cancel current transfer if underway- Specified by:
cancelAllTransfersin interfaceFileTransferClientInterface
-
getLastReply
Gets the last reply from the server, whether valid or not- Returns:
- reply object encapsulating last server response
-
getSystemType
Get a string that represents the remote system that the client is logged into.- Specified by:
getSystemTypein interfaceFileTransferClientInterface- Returns:
- system string
- Throws:
FTPExceptionIOException
-
directoryNameList
List the names of files and directories in the current directory on the FTP server. Most servers return an empty directory listing if a directory does not exist, and this is also what this method does. To establish whether or not a directory exists, try changing into it.- Specified by:
directoryNameListin interfaceFileTransferClientInterface- Returns:
- String[]
- Throws:
FTPExceptionIOException
-
directoryNameList
public String[] directoryNameList(String directoryName, boolean isLongListing) throws FTPException, IOException List the names of files and directories of a directory on the FTP server. Most servers return an empty directory listing if a directory does not exist, and this is also what this method does. To establish whether or not a directory exists, try changing into it.- Specified by:
directoryNameListin interfaceFileTransferClientInterface- Parameters:
directoryName- name of the directory (generally not a path). Some servers will accept a wildcard.isLongListing- true if the listing is a long format listing- Returns:
- String[]
- Throws:
FTPExceptionIOException
-
directoryList
List the current directory on the FTP server. Most servers return an empty directory listing if a directory does not exist, and this is also what this method does. To establish whether or not a directory exists, try changing into it.- Specified by:
directoryListin interfaceFileTransferClientInterface- Throws:
ParseExceptionFTPExceptionIOException
-
directoryList
public FTPFile[] directoryList(String directoryName) throws FTPException, IOException, ParseException List a directory on the FTP server. Most servers return an empty directory listing if a directory does not exist, and this is also what this method does. To establish whether or not a directory exists, try changing into it.- Specified by:
directoryListin interfaceFileTransferClientInterface- Parameters:
directoryName- name of the directory (generally not a path). Some servers will accept a wildcard.- Throws:
ParseExceptionFTPExceptionIOException
-
directoryList
public void directoryList(String directoryName, DirectoryListCallback lister) throws FTPException, IOException, ParseException List a directory on the FTP server. Most servers return an empty directory listing if a directory does not exist, and this is also what this method does. To establish whether or not a directory exists, try changing into it.- Specified by:
directoryListin interfaceFileTransferClientInterface- Parameters:
directoryName- name of the directory (generally not a path)- Throws:
FTPExceptionIOExceptionParseException
-
downloadByteArray
Download a file from the FTP server into a byte array.- Specified by:
downloadByteArrayin interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of the remote file to be downloaded- Throws:
FTPExceptionIOException
-
downloadFile
public void downloadFile(String localFileName, String remoteFileName) throws FTPException, IOException Download a file from the FTP server .- Specified by:
downloadFilein interfaceFileTransferClientInterface- Parameters:
localFileName- name (or full path) of the local file to be downloaded toremoteFileName- name of the remote file to be downloaded- Throws:
FTPExceptionIOException
-
downloadFile
public void downloadFile(String localFileName, String remoteFileName, WriteMode writeMode) throws FTPException, IOException Download a file from the FTP server .- Specified by:
downloadFilein interfaceFileTransferClientInterface- Parameters:
localFileName- name (or full path) of the local file to be downloaded toremoteFileName- name of the remote file to be downloadedwriteMode- mode in which the file is written to the client machine- Throws:
FTPExceptionIOException
-
downloadURLStream
public static FileTransferInputStream downloadURLStream(String ftpURL) throws MalformedURLException, IOException, FTPException Open an InputStream for the given FTP URL. The URL must be of the formftp://[user[:password]@]host[:port]/path. The stream should be closed when the transfer is complete.- Parameters:
ftpURL- The URL must be of the formftp://[user[:password]@]host[:port]/path.- Returns:
- FileTransferInputStream for the given URL. The stream should be closed when the transfer is complete.
- Throws:
MalformedURLException- Thrown when an invalid URL is provided.IOException- Thrown when an I/O-related error occurs.FTPException- Thrown when an FTP-protocol-related error occurs.
-
downloadURLFile
public static void downloadURLFile(String localFileName, String ftpURL) throws MalformedURLException, IOException, FTPException Downloads the given file to the given FTP URL. The URL must be of the formftp://[user[:password]@]host[:port]/path.- Parameters:
localFileName- Path to local fileftpURL- The URL must be of the formftp://[user[:password]@]host[:port]/path.- Throws:
MalformedURLException- Thrown when an invalid URL is provided.IOException- Thrown when an I/O-related error occurs.FTPException- Thrown when an FTP-protocol-related error occurs.
-
downloadStream
public FileTransferInputStream downloadStream(String remoteFileName) throws FTPException, IOException Download a file from the FTP server as a stream. The close() method must be called on the stream once the stream has been read.- Specified by:
downloadStreamin interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of the remote file to be downloaded- Returns:
- InputStream
- Throws:
FTPExceptionIOException
-
uploadFile
public String uploadFile(String localFileName, String remoteFileName) throws FTPException, IOException Upload a file to the FTP server. If a null is supplied as the remoteFileName, the STOU (store unique) FTP feature will be used (if available) and the FTP server will generate a unique name for the file.- Specified by:
uploadFilein interfaceFileTransferClientInterface- Parameters:
localFileName- name (or full path) of the local file to be downloaded toremoteFileName- name of the remote file to be downloaded (or null to generate a unique name)- Returns:
- name of remote file
- Throws:
FTPExceptionIOException
-
uploadFile
public String uploadFile(String localFileName, String remoteFileName, WriteMode writeMode) throws FTPException, IOException Upload a file to the FTP server. If a null is supplied as the remoteFileName, the STOU (store unique) FTP feature will be used (if available) and the FTP server will generate a unique name for the file.- Specified by:
uploadFilein interfaceFileTransferClientInterface- Parameters:
localFileName- name (or full path) of the local file to be downloaded toremoteFileName- name of the remote file to be downloaded (or null to generate a unique name)writeMode- mode to write to the remote file with- Returns:
- name of remote file
- Throws:
FTPExceptionIOException
-
uploadStream
public FileTransferOutputStream uploadStream(String remoteFileName) throws FTPException, IOException Upload a file to the FTP server by writing to a stream. The close() method must be called on the stream once the stream has been read. If a null is supplied as the remoteFileName, the STOU (store unique) FTP feature will be used (if available) and the FTP server will generate a unique name for the file. This name can be obtained afterwards from- Specified by:
uploadStreamin interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of the remote file to be uploaded- Returns:
- FileTransferOutputStream
- Throws:
FTPExceptionIOException
-
uploadStream
public FileTransferOutputStream uploadStream(String remoteFileName, WriteMode writeMode) throws FTPException, IOException Upload a file to the FTP server by writing to a stream. The close() method *must* be called on the stream once the stream has been read. If a null is supplied as the remoteFileName, the STOU (store unique) FTP feature will be used (if available) and the FTP server will generate a unique name for the file. This name can be obtained afterwards from- Specified by:
uploadStreamin interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of the remote file to be uploadedwriteMode- mode for writing to the server (supporting use of append)- Returns:
- FileTransferOutputStream
- Throws:
FTPExceptionIOException
-
uploadURLStream
public static FileTransferOutputStream uploadURLStream(String ftpURL) throws MalformedURLException, IOException, FTPException Open an OutputStream for the given FTP URL. The URL must be of the formftp://[user[:password]@]host[:port]/path. The stream should be closed when the transfer is complete.- Parameters:
ftpURL- The URL must be of the formftp://[user[:password]@]host[:port]/path.- Returns:
- FileTransferOutputStream for the given URL. The stream should be closed when the transfer is complete.
- Throws:
MalformedURLException- Thrown when an invalid URL is provided.IOException- Thrown when an I/O-related error occurs.FTPException- Thrown when an FTP-protocol-related error occurs.
-
uploadURLFile
public static void uploadURLFile(String localFileName, String ftpURL) throws MalformedURLException, IOException, FTPException Uploads the given file to the given FTP URL. The URL must be of the formftp://[user[:password]@]host[:port]/path.- Parameters:
localFileName- Path to local fileftpURL- The URL must be of the formftp://[user[:password]@]host[:port]/path.- Throws:
MalformedURLException- Thrown when an invalid URL is provided.IOException- Thrown when an I/O-related error occurs.FTPException- Thrown when an FTP-protocol-related error occurs.
-
getSize
Get the size of a remote file.- Specified by:
getSizein interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of remote file- Returns:
- long
- Throws:
FTPExceptionIOException
-
getModifiedTime
Get the modified-time of a remote file.- Specified by:
getModifiedTimein interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of remote file- Returns:
- Date
- Throws:
FTPExceptionIOException
-
setModifiedTime
public void setModifiedTime(String remoteFileName, Date modifiedTime) throws FTPException, IOException Set the modified-time of a remote file. May not be supported by all servers.- Specified by:
setModifiedTimein interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of remote filemodifiedTime- new modified time- Throws:
FTPExceptionIOException
-
exists
Determine if a remote file exists.- Specified by:
existsin interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of remote file- Throws:
FTPExceptionIOException
-
deleteFile
Deletes a remote file.- Specified by:
deleteFilein interfaceFileTransferClientInterface- Parameters:
remoteFileName- name of remote file- Throws:
FTPExceptionIOException
-
rename
Rename a remote file or directory.- Specified by:
renamein interfaceFileTransferClientInterface- Parameters:
renameFromName- original namerenameToName- new name- Throws:
FTPExceptionIOException
-
changeDirectory
Change directory on the FTP server.- Specified by:
changeDirectoryin interfaceFileTransferClientInterface- Parameters:
directoryName- name the remote directory to change into- Throws:
FTPExceptionIOException
-
changeToParentDirectory
Change to parent directory on the FTP server.- Specified by:
changeToParentDirectoryin interfaceFileTransferClientInterface- Throws:
FTPExceptionIOException
-
getRemoteDirectory
Get the current remote directory.- Specified by:
getRemoteDirectoryin interfaceFileTransferClientInterface- Returns:
- current remote directory
- Throws:
IOExceptionFTPException
-
createDirectory
Create directory on the FTP server.- Specified by:
createDirectoryin interfaceFileTransferClientInterface- Parameters:
directoryName- name the remote directory to create- Throws:
FTPExceptionIOException
-
deleteDirectory
Delete directory on the FTP server. The directory must be empty. Note that edtFTPj/PRO supports recursive directory deletions.- Specified by:
deleteDirectoryin interfaceFileTransferClientInterface- Parameters:
directoryName- name the remote directory to create- Throws:
FTPExceptionIOException
-
disconnect
Disconnect from the FTP server.- Specified by:
disconnectin interfaceFileTransferClientInterface- Throws:
FTPExceptionIOException
-
disconnect
Disconnect from the FTP server.- Specified by:
disconnectin interfaceFileTransferClientInterface- Throws:
FTPExceptionIOException
-