Package com.compomics.util.io.ftp
Class FTP
java.lang.Object
com.compomics.util.io.ftp.FTP
This class was modified from a source found on the net (java.sun.com, search in
developer section on 'FtpProtocolException PORT').
- Author:
- Lennart Martens + someone on the net...
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe last command issued.protected SocketSocket for communicating commands with the server.static intPre-defined state.static intPre-defined state.static final intFTP port to use for connection.protected Stringprotected StringBuffered stream for reading replies from server.Stream for printing to the server.protected VectorArray of strings (usually 1 entry) for the last reply from the server.protected StringString to hold the file we are up/downloadingWelcome message from the server, if any. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Send Abort command.appendAscii(String filename) APPEND (with create) to a file to the FTP server in Ascii mode.appendBinary(String filename) APPEND (with create) to a file to the FTP server in Binary mode.voidascii()Set transfer type to 'A' (ascii transfer).voidbinary()Set transfer type to 'I' (binary transfer).voidCD to a specific directory on a remote FTP server.voidcdup()Go up one directory on remote system.voidIssue the QUIT command to the FTP server and close the connection.voidChange working directory to a specific directory on a remote FTP server.voidDelete the specified file from the ftp server file system.dir()Folder-list files on a remote FTP server.GET a file from the FTP server in Ascii mode.GET a file from the FTP server in Binary mode.This method reports on the destinationfile currently used in transfer.Returns all server response strings.This method allows the caller to read the response strings from the server, without resetting the internal buffer (and thus not clearing the messages read by this messages).This method reports on the sourcefile currently used in transfer.protected intissueCommand(String cmd) This method allows the caller to issue a command to the server.protected voidissueCommandCheck(String cmd) This method will issue the specified command and throw an exception whenever the reply is not equal to success! It basically converts an FTP error code into an FtpProtocolException.list()LIST files on a remote FTP server.voidLogin user to a host with username user and no password such as HP server which uses the form "<username>/<password>,user.<group>.voidLogin user to a host with username user and password password.ls()Folder-list files on a remote FTP server.voidCreate a directory on the remote systemnlist()NLIST files on a remote FTP serverprotected SocketopenDataConnection(String cmd) This method will set up the networking for client-server data transfer and it will send the specified command to the server.voidopenServer(String host) open a FTP connection to host host.voidopenServer(String host, int port) Open an FTP connection to host host on port port.PUT a file on the FTP server in Ascii mode.PUT a file to the FTP server in Binary modevoidpwd()Get the name of the present working directory on the ftp server file system.protected intThis method attempts to read a reply from the FTP server.intThis method allows the caller to capture the server response.voidRename a file on the remote server.voidDelete the specified directory from the ftp server file system.voidsendServer(String cmd) Sends command cmd to the server.booleanReturn server connection statusvoidsetDestinationFile(String strDestinationFile) This method allows the specification of the destinationfile.voidSetFileMode(int nMode) This method sets the file transfer mode.voidsetFilename(String strFile) This method sets the name of the file to up- or download.voidsetPassive(boolean mode) Set Passive mode Transfers.voidsetSourceFile(String strSourceFile) This method allows to set the source for the file that is to be transferred.voidSite Commandvoidsyst()Retrieve the system type from the remote server.
-
Field Details
-
FTP_PORT
public static final int FTP_PORTFTP port to use for connection.- See Also:
-
FILE_GET
public static int FILE_GETPre-defined state. -
FILE_PUT
public static int FILE_PUTPre-defined state. -
command
The last command issued. -
welcomeMsg
Welcome message from the server, if any. -
serverResponse
Array of strings (usually 1 entry) for the last reply from the server. -
controlSocket
Socket for communicating commands with the server. -
serverOutput
Stream for printing to the server. -
serverInput
Buffered stream for reading replies from server. -
strFileNameAndPath
String to hold the file we are up/downloading -
m_strSource
-
m_strDestination
-
-
Constructor Details
-
FTP
public FTP()Empty default constructor -
FTP
Constructor for an FTP client connected to host host. Note that this constructor automatically makes the connection.- Parameters:
host- String with the hostname (or IP) to connect to.- Throws:
IOException- whenever a connection could not be made.
-
FTP
Constructor for an FTP client connected to host host and port port. Note that this constructor automatically makes the connection.- Parameters:
host- String with the hostname (or IP) to connect to.port- int with the portnumber for the host FTP server.- Throws:
IOException- whenever a connection could not be made.
-
-
Method Details
-
setFilename
This method sets the name of the file to up- or download.- Parameters:
strFile- String with the filename.
-
setSourceFile
This method allows to set the source for the file that is to be transferred.- Parameters:
strSourceFile- String with the sourec for the file to be transferred.
-
setDestinationFile
This method allows the specification of the destinationfile.- Parameters:
strDestinationFile- String with the destination file.
-
getSourceFile
This method reports on the sourcefile currently used in transfer.- Returns:
- String with the source file.
-
getDestinationFile
This method reports on the destinationfile currently used in transfer.- Returns:
- String with the destination file.
-
serverIsOpen
public boolean serverIsOpen()Return server connection status- Returns:
- boolean 'true' when connected, 'false' otherwise.
-
setPassive
public void setPassive(boolean mode) Set Passive mode Transfers. This is particularly useful when attempting to FTP via passive mode.- Parameters:
mode- boolean to indicate whether passive mode should be used.
-
readServerResponse
This method allows the caller to capture the server response.- Returns:
- int with the server's response.
- Throws:
IOException- if an IOException occurs
-
sendServer
Sends command cmd to the server.- Parameters:
cmd- String with the command to send.
-
getResponseString
Returns all server response strings. It also clears the server-response buffer!- Returns:
- String with all the server response strings.
-
getResponseStringNoReset
This method allows the caller to read the response strings from the server, without resetting the internal buffer (and thus not clearing the messages read by this messages).- Returns:
- String with the server responses.
-
closeServer
Issue the QUIT command to the FTP server and close the connection.- Throws:
IOException- if an IOException occurs
-
issueCommand
This method allows the caller to issue a command to the server.- Parameters:
cmd- String with the command to issue.- Returns:
- int with the server reply status.
- Throws:
IOException- when the connection with the server failed.
-
issueCommandCheck
This method will issue the specified command and throw an exception whenever the reply is not equal to success! It basically converts an FTP error code into an FtpProtocolException.- Parameters:
cmd- String with the command to issue (and verify the response for)- Throws:
IOException- when the connection failed OR FtpProtocolException when the command was not understood by the server.
-
readReply
This method attempts to read a reply from the FTP server.- Returns:
- int with the reply from the server.
- Throws:
IOException- whenever the reply could not be read.
-
openDataConnection
This method will set up the networking for client-server data transfer and it will send the specified command to the server. This is typically used only for true file transfer.- Parameters:
cmd- String with the command to issue.- Returns:
- Socket with the data connection socket.
- Throws:
IOException- whenever communications could not be established.
-
openServer
open a FTP connection to host host.- Parameters:
host- String with the hostname (or IP) to connect to.- Throws:
IOException- whenever connection could not be established.UnknownHostException- when the hostname cannot be resolved.
-
openServer
Open an FTP connection to host host on port port. This method can be used whenever the default FTP port (21) does not apply.- Parameters:
host- String with the hostname (or IP) of the FTP server.port- int with the portnumber the FTP server is listening on.- Throws:
IOException- whenever connection could not be established.UnknownHostException- when the hostname cannot be resolved.
-
login
Login user to a host with username user and password password.- Parameters:
user- String with the username to use.password- String with the password to use (passwords are sent in plain text in FTP!)- Throws:
IOException- if an IOException occurs
-
login
Login user to a host with username user and no password such as HP server which uses the form "<username>/<password>,user.<group>.- Parameters:
user- String with the username (and possibly coded information such as password).- Throws:
IOException- if an IOException occurs
-
getAscii
GET a file from the FTP server in Ascii mode.- Parameters:
filename- String with the filename to get from the server.- Returns:
- BufferedReader to the file on the server.
- Throws:
IOException- whenever the file could not be read.
-
getBinary
GET a file from the FTP server in Binary mode.- Parameters:
filename- String with the filename to get from the server.- Returns:
- BufferedInputStream to the file on the server.
- Throws:
IOException- whenever the file could not be read.
-
putAscii
PUT a file on the FTP server in Ascii mode.- Parameters:
filename- String with the filename to put on the server.- Returns:
- BufferedWriter writer for completing the file on the server.
- Throws:
IOException- whenever the file could not be sent.
-
putBinary
PUT a file to the FTP server in Binary mode- Parameters:
filename- String with the filename to put on the server.- Returns:
- BufferedOutputStream outputstream for completing the file on the server.
- Throws:
IOException- whenever the file could not be sent.
-
appendAscii
APPEND (with create) to a file to the FTP server in Ascii mode.- Parameters:
filename- String with the name of the file to append to.- Returns:
- BufferedWriter with the stream for appending to.
- Throws:
IOException- whenever the writer fails.
-
appendBinary
APPEND (with create) to a file to the FTP server in Binary mode.- Parameters:
filename- String with the name of the file to append to.- Returns:
- BufferedOutputStream with the stream for appending to.
- Throws:
IOException- whenever the writer fails.
-
nlist
NLIST files on a remote FTP server- Returns:
- BufferedReader to read the listing from.
- Throws:
IOException- whenever the listing failed.
-
list
LIST files on a remote FTP server.- Returns:
- BufferedReader to read the listing from.
- Throws:
IOException- whenever the listing failed.
-
ls
Folder-list files on a remote FTP server.- Returns:
- BufferedReader to read the listing from.
- Throws:
IOException- whenever the listing failed.
-
dir
Folder-list files on a remote FTP server.- Returns:
- BufferedReader to read the listing from.
- Throws:
IOException- whenever the listing failed.
-
cd
CD to a specific directory on a remote FTP server.- Parameters:
remoteDirectory- String with the directory to CD to.- Throws:
IOException- whenever the CD failed.
-
cwd
Change working directory to a specific directory on a remote FTP server.- Parameters:
remoteDirectory- String with the directory to CWD to.- Throws:
IOException- whenever the CWD failed.
-
rename
Rename a file on the remote server.- Parameters:
oldFile- String with the original filename for the file to rename.newFile- String with the filename to rename the file to.- Throws:
IOException- when the renaming failed.
-
site
Site Command- Parameters:
params- String with the parameters for the SITE command.- Throws:
IOException- when the SITE command failed.
-
binary
Set transfer type to 'I' (binary transfer).- Throws:
IOException- when the binary mode could not be set up.
-
ascii
Set transfer type to 'A' (ascii transfer).- Throws:
IOException- when the ASCII mode could not be set up.
-
abort
Send Abort command.- Throws:
IOException- when the cancellation could not be executed.
-
cdup
Go up one directory on remote system.- Throws:
IOException- when the CDUP failed.
-
mkdir
Create a directory on the remote system- Parameters:
aDir- String with the name for the directory to be created.- Throws:
IOException- when the directory could not be created.
-
rmdir
Delete the specified directory from the ftp server file system.- Parameters:
aDir- String with the directory to delete.- Throws:
IOException- when the deletion did not succeed.
-
delete
Delete the specified file from the ftp server file system.- Parameters:
aFile- String with the filename for the file to delete.- Throws:
IOException- when the deletion did not succeed.
-
pwd
Get the name of the present working directory on the ftp server file system.- Throws:
IOException- whenever the server did not report on the pwd.
-
syst
Retrieve the system type from the remote server.- Throws:
IOException- whenever the system type could not be determined.
-
SetFileMode
public void SetFileMode(int nMode) This method sets the file transfer mode.- Parameters:
nMode- int with the mode (either FILE_GET for retrieval, or any other int for sending a file).
-