com.compomics.util.io
Class FTP

java.lang.Object
  extended by com.compomics.util.io.FTP

public class FTP
extends java.lang.Object

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
 java.lang.String command
          The last command issued.
protected  java.net.Socket controlSocket
          Socket for communicating commands with the server.
static int FILE_GET
          Pre-defined state.
static int FILE_PUT
          Pre-defined state.
static int FTP_PORT
          FTP port to use for connection.
protected  java.lang.String m_strDestination
           
protected  java.lang.String m_strSource
           
 java.io.InputStream serverInput
          Buffered stream for reading replies from server.
 java.io.PrintWriter serverOutput
          Stream for printing to the server.
protected  java.util.Vector serverResponse
          Array of strings (usually 1 entry) for the last reply from the server.
protected  java.lang.String strFileNameAndPath
          String to hold the file we are up/downloading
 java.lang.String welcomeMsg
          Welcome message from the server, if any.
 
Constructor Summary
FTP(java.lang.String host)
          Constructor for an FTP client connected to host host.
FTP(java.lang.String host, int port)
          Constructor for an FTP client connected to host host and port port.
 
Method Summary
 void abort()
          Send Abort command.
 java.io.BufferedWriter appendAscii(java.lang.String filename)
          APPEND (with create) to a file to the FTP server in Ascii mode.
 java.io.BufferedOutputStream appendBinary(java.lang.String filename)
          APPEND (with create) to a file to the FTP server in Binary mode.
 void ascii()
          Set transfer type to 'A' (ascii transfer).
 void binary()
          Set transfer type to 'I' (binary transfer).
 void cd(java.lang.String remoteDirectory)
          CD to a specific directory on a remote FTP server.
 void cdup()
          Go up one directory on remote system.
 void closeServer()
          Issue the QUIT command to the FTP server and close the connection.
 void cwd(java.lang.String remoteDirectory)
          Change working directory to a specific directory on a remote FTP server.
 void delete(java.lang.String aFile)
          Delete the specified file from the ftp server file system.
 java.io.BufferedReader dir()
          Folder-list files on a remote FTP server.
 java.io.BufferedReader getAscii(java.lang.String filename)
          GET a file from the FTP server in Ascii mode.
 java.io.BufferedInputStream getBinary(java.lang.String filename)
          GET a file from the FTP server in Binary mode.
 java.lang.String getDestinationFile()
          This method reports on the destinationfile currently used in transfer.
 java.lang.String getResponseString()
          Returns all server response strings.
 java.lang.String 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).
 java.lang.String getSourceFile()
          This method reports on the sourcefile currently used in transfer.
protected  int issueCommand(java.lang.String cmd)
          This method allows the caller to issue a command to the server.
protected  void issueCommandCheck(java.lang.String cmd)
          This method will issue the specified command and throw an exception whenever the reply is not equal to success!
 java.io.BufferedReader list()
          LIST files on a remote FTP server.
 void login(java.lang.String user)
          Login user to a host with username user and no password such as HP server which uses the form "/,user.
 void login(java.lang.String user, java.lang.String password)
          Login user to a host with username user and password password.
 java.io.BufferedReader ls()
          Folder-list files on a remote FTP server.
 void mkdir(java.lang.String aDir)
          Create a directory on the remote system
 java.io.BufferedReader nlist()
          NLIST files on a remote FTP server
protected  java.net.Socket openDataConnection(java.lang.String cmd)
          This method will set up the networking for client-server data transfer and it will send the specified command to the server.
 void openServer(java.lang.String host)
          open a FTP connection to host host.
 void openServer(java.lang.String host, int port)
          Open an FTP connection to host host on port port.
 java.io.BufferedWriter putAscii(java.lang.String filename)
          PUT a file on the FTP server in Ascii mode.
 java.io.BufferedOutputStream putBinary(java.lang.String filename)
          PUT a file to the FTP server in Binary mode
 void pwd()
          Get the name of the present working directory on the ftp server file system.
protected  int readReply()
          This method attempts to read a reply from the FTP server.
 int readServerResponse()
          This method allows the caller to capture the serverresponse.
 void rename(java.lang.String oldFile, java.lang.String newFile)
          Rename a file on the remote server.
 void rmdir(java.lang.String aDir)
          Delete the specified directory from the ftp server file system.
 void sendServer(java.lang.String cmd)
          Sends command cmd to the server.
 boolean serverIsOpen()
          Return server connection status
 void setDestinationFile(java.lang.String strDestinationFile)
          This method allows the specification of the destinationfile.
 void SetFileMode(int nMode)
          This method sets the file transfer mode.
 void setFilename(java.lang.String strFile)
          This method sets the name of the file to up- or download.
 void setPassive(boolean mode)
          Set Passive mode Trasfers.
 void setSourceFile(java.lang.String strSourceFile)
          This method allows to set the source for the file that is to be transferred.
 void site(java.lang.String params)
          Site Command
 void syst()
          Retrieve the system type from the remote server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FTP_PORT

public static final int FTP_PORT
FTP port to use for connection.

See Also:
Constant Field Values

FILE_GET

public static int FILE_GET
Pre-defined state.


FILE_PUT

public static int FILE_PUT
Pre-defined state.


command

public java.lang.String command
The last command issued.


welcomeMsg

public java.lang.String welcomeMsg
Welcome message from the server, if any.


serverResponse

protected java.util.Vector serverResponse
Array of strings (usually 1 entry) for the last reply from the server.


controlSocket

protected java.net.Socket controlSocket
Socket for communicating commands with the server.


serverOutput

public java.io.PrintWriter serverOutput
Stream for printing to the server.


serverInput

public java.io.InputStream serverInput
Buffered stream for reading replies from server.


strFileNameAndPath

protected java.lang.String strFileNameAndPath
String to hold the file we are up/downloading


m_strSource

protected java.lang.String m_strSource

m_strDestination

protected java.lang.String m_strDestination
Constructor Detail

FTP

public FTP(java.lang.String host)
    throws java.io.IOException
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:
java.io.IOException - whenever a connection could not be made.

FTP

public FTP(java.lang.String host,
           int port)
    throws java.io.IOException
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:
java.io.IOException - whenever a connection could not be made.
Method Detail

setFilename

public void setFilename(java.lang.String strFile)
This method sets the name of the file to up- or download.

Parameters:
strFile - String with the filename.

setSourceFile

public void setSourceFile(java.lang.String strSourceFile)
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

public void setDestinationFile(java.lang.String strDestinationFile)
This method allows the specification of the destinationfile.

Parameters:
strDestinationFile - String with the destination file.

getSourceFile

public java.lang.String getSourceFile()
This method reports on the sourcefile currently used in transfer.

Returns:
String with the source file.

getDestinationFile

public java.lang.String 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 Trasfers. This is particularly useful when attempting to FTP via passive mode.

Parameters:
mode - boolean to indicate whether passive mode should be used.

readServerResponse

public int readServerResponse()
                       throws java.io.IOException
This method allows the caller to capture the serverresponse.

Returns:
int with the server's response.
Throws:
java.io.IOException

sendServer

public void sendServer(java.lang.String cmd)
Sends command cmd to the server.

Parameters:
cmd - String with the command to send.

getResponseString

public java.lang.String getResponseString()
Returns all server response strings. It also clears the server-response buffer!

Returns:
String with all the server response strings.

getResponseStringNoReset

public java.lang.String 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

public void closeServer()
                 throws java.io.IOException
Issue the QUIT command to the FTP server and close the connection.

Throws:
java.io.IOException

issueCommand

protected int issueCommand(java.lang.String cmd)
                    throws java.io.IOException
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:
java.io.IOException - when the connection with the server failed.

issueCommandCheck

protected void issueCommandCheck(java.lang.String cmd)
                          throws java.io.IOException
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:
java.io.IOException - when the connection failed OR FtpProtocolException when the command was not understood by the server.

readReply

protected int readReply()
                 throws java.io.IOException
This method attempts to read a reply from the FTP server.

Returns:
int with the reply from the server.
Throws:
java.io.IOException - whenever the reply could not be read.

openDataConnection

protected java.net.Socket openDataConnection(java.lang.String cmd)
                                      throws java.io.IOException
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:
java.io.IOException - whenever communications could not be established.

openServer

public void openServer(java.lang.String host)
                throws java.io.IOException,
                       java.net.UnknownHostException
open a FTP connection to host host.

Parameters:
host - String with the hostname (or IP) to connect to.
Throws:
java.io.IOException - whenever connection could not be established.
java.net.UnknownHostException - when the hostname cannot be resolved.

openServer

public void openServer(java.lang.String host,
                       int port)
                throws java.io.IOException,
                       java.net.UnknownHostException
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:
java.io.IOException - whenever connection could not be established.
java.net.UnknownHostException - when the hostname cannot be resolved.

login

public void login(java.lang.String user,
                  java.lang.String password)
           throws java.io.IOException
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:
java.io.IOException

login

public void login(java.lang.String user)
           throws java.io.IOException
Login user to a host with username user and no password such as HP server which uses the form "/,user..

Parameters:
user - String with the username (and possibly coded information such as password).
Throws:
java.io.IOException

getAscii

public java.io.BufferedReader getAscii(java.lang.String filename)
                                throws java.io.IOException
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:
java.io.IOException - whenever the file could not be read.

getBinary

public java.io.BufferedInputStream getBinary(java.lang.String filename)
                                      throws java.io.IOException
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:
java.io.IOException - whenever the file could not be read.

putAscii

public java.io.BufferedWriter putAscii(java.lang.String filename)
                                throws java.io.IOException
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:
java.io.IOException - whenever the file could not be sent.

putBinary

public java.io.BufferedOutputStream putBinary(java.lang.String filename)
                                       throws java.io.IOException
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:
java.io.IOException - whenever the file could not be sent.

appendAscii

public java.io.BufferedWriter appendAscii(java.lang.String filename)
                                   throws java.io.IOException
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:
java.io.IOException - whenever the writer fails.

appendBinary

public java.io.BufferedOutputStream appendBinary(java.lang.String filename)
                                          throws java.io.IOException
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:
java.io.IOException - whenever the writer fails.

nlist

public java.io.BufferedReader nlist()
                             throws java.io.IOException
NLIST files on a remote FTP server

Returns:
BufferedReader to read the listing from.
Throws:
java.io.IOException - whenever the listing failed.

list

public java.io.BufferedReader list()
                            throws java.io.IOException
LIST files on a remote FTP server.

Returns:
BufferedReader to read the listing from.
Throws:
java.io.IOException - whenever the listing failed.

ls

public java.io.BufferedReader ls()
                          throws java.io.IOException
Folder-list files on a remote FTP server.

Returns:
BufferedReader to read the listing from.
Throws:
java.io.IOException - whenever the listing failed.

dir

public java.io.BufferedReader dir()
                           throws java.io.IOException
Folder-list files on a remote FTP server.

Returns:
BufferedReader to read the listing from.
Throws:
java.io.IOException - whenever the listing failed.

cd

public void cd(java.lang.String remoteDirectory)
        throws java.io.IOException
CD to a specific directory on a remote FTP server.

Parameters:
remoteDirectory - String with the directory to CD to.
Throws:
java.io.IOException - whenever the CD failed.

cwd

public void cwd(java.lang.String remoteDirectory)
         throws java.io.IOException
Change working directory to a specific directory on a remote FTP server.

Parameters:
remoteDirectory - String with the directory to CWD to.
Throws:
java.io.IOException - whenever the CWD failed.

rename

public void rename(java.lang.String oldFile,
                   java.lang.String newFile)
            throws java.io.IOException
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:
java.io.IOException - when the renaming failed.

site

public void site(java.lang.String params)
          throws java.io.IOException
Site Command

Parameters:
params - String with the parameters for the SITE command.
Throws:
java.io.IOException - when the SITE command failed.

binary

public void binary()
            throws java.io.IOException
Set transfer type to 'I' (binary transfer).

Throws:
java.io.IOException - when the binary mode could not be set up.

ascii

public void ascii()
           throws java.io.IOException
Set transfer type to 'A' (ascii transfer).

Throws:
java.io.IOException - when the ASCII mode could not be set up.

abort

public void abort()
           throws java.io.IOException
Send Abort command.

Throws:
java.io.IOException - when the cancellation could not be executed.

cdup

public void cdup()
          throws java.io.IOException
Go up one directory on remote system.

Throws:
java.io.IOException - when the CDUP failed.

mkdir

public void mkdir(java.lang.String aDir)
           throws java.io.IOException
Create a directory on the remote system

Parameters:
aDir - String with the name for the directory to be created.
Throws:
java.io.IOException - when the directory could not be created.

rmdir

public void rmdir(java.lang.String aDir)
           throws java.io.IOException
Delete the specified directory from the ftp server file system.

Parameters:
aDir - String with the directory to delete.
Throws:
java.io.IOException - when the deletion did not succeed.

delete

public void delete(java.lang.String aFile)
            throws java.io.IOException
Delete the specified file from the ftp server file system.

Parameters:
aFile - String with the filename for the file to delete.
Throws:
java.io.IOException - when the deletion did not succeed.

pwd

public void pwd()
         throws java.io.IOException
Get the name of the present working directory on the ftp server file system.

Throws:
java.io.IOException - whenever the server did not report on the pwd.

syst

public void syst()
          throws java.io.IOException
Retrieve the system type from the remote server.

Throws:
java.io.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).


Copyright © 2011. All Rights Reserved.