Class FTPClient


  • public class FTPClient
    extends java.lang.Object
    This class represents a custom FTP client, that has the functionality to connect to a FTP server, to login, list contents, transfer data, etc.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.lang.String COPYRIGHT
      Copyright information.
    • Constructor Summary

      Constructors 
      Constructor Description
      FTPClient()
      Default constructor.
      FTPClient​(Log logger)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void checkComplete()
      Checks whether the file transfer (when getting/putting a file on the FTP server) is over.
      void connect​(java.lang.String host, int port, boolean useSSLonCommandChannel, boolean useSSLonDataChannel)
      Creates a connection to the given host and port.
      void connect​(java.lang.String host, int port, boolean useSSLonCommandChannel, boolean useSSLonDataChannel, boolean useExplicitModeSSL)
      Creates a connection to the given host and port.
      void cwd​(java.lang.String cwd)
      Changes the working directory with the one specified.
      void deleteFile​(java.lang.String remoteFile)
      Deletes a given file from the FTP server.
      java.lang.String[] dir()
      Returns a long list of the contents of the current working directory on the FTP server.
      java.lang.String[] dir​(java.lang.String path, boolean longlisting)
      Returns a string array with the contents of the given path.
      void disconnect()
      Disconnects the client from the FTP server.
      boolean getDebug()
      Gets the debug level used.
      java.net.Socket getFile​(java.lang.String remoteFile, boolean binary)
      Retrieves the specified file from the FTP server.
      void getFile​(java.lang.String remoteFile, java.lang.String localFile, boolean binary)
      Retrieves the specified file from the FTP server.
      java.lang.String getResponse()
      Retrieves the server response to a previously sent by the client command.
      boolean getUsePassive()
      Returns true if we should try to use passive mode.
      java.lang.String[] list()
      Returns a short list of the contents of the current working directory on the FTP server.
      java.net.Socket list​(java.lang.String path, boolean longlisting)
      Lists the contents of a given path on the FTP server.
      void login​(java.lang.String user, java.lang.String password)
      Logs to the FTP server using the given credentials.
      java.lang.String mkdir​(java.lang.String dir)
      Creates the directory
      java.net.Socket putFile​(java.lang.String remoteFile, boolean binary)
      Sends a local file to the server.
      void putFile​(java.lang.String localPath, java.lang.String remoteFile, boolean binary)
      Sends a local file to the server.
      java.lang.String pwd()
      Get the current remote working directory.
      void rename​(java.lang.String from, java.lang.String to)
      Renames a file or directory on the FTP server.
      java.lang.String sendCommand​(java.lang.String cmd)
      Sends the given command through the command channel.
      void setDebug​(boolean debug)
      Sets the client in detailed logging mode.
      void setTimeout​(int millis)
      This method does nothing.
      void setTransferMode​(boolean binary)
      Sets the transfer mode according to the type of files to be transfered.
      void setupDataSocket()
      Creates the data socket used for data transferring between the FTP client and server.
      void setUsePassive​(boolean value)
      Sets this FTPClient to use passive mode (or not).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • COPYRIGHT

        protected static final java.lang.String COPYRIGHT
        Copyright information.
        See Also:
        Constant Field Values
    • Constructor Detail

      • FTPClient

        public FTPClient()
        Default constructor.
      • FTPClient

        public FTPClient​(Log logger)
        Constructor.
        Parameters:
        logger - a Log object used for logging debug messages.
    • Method Detail

      • setTimeout

        public void setTimeout​(int millis)
        This method does nothing.
        Parameters:
        millis - the timeout to be set in milliseconds.
      • setDebug

        public void setDebug​(boolean debug)
        Sets the client in detailed logging mode.
        Parameters:
        debug - whether to log in details or not.
      • getDebug

        public boolean getDebug()
        Gets the debug level used.
        Returns:
        whether the client is in detail logging mode.
      • connect

        public void connect​(java.lang.String host,
                            int port,
                            boolean useSSLonCommandChannel,
                            boolean useSSLonDataChannel)
                     throws java.lang.Exception
        Creates a connection to the given host and port. The security of the two channels established is determined by the other two input parameters.
        Parameters:
        host - the host to connect to.
        port - the port to connect to.
        useSSLonCommandChannel - whether to use SSL on the command channel.
        useSSLonDataChannel - whether to use SSL on the data channel.
        Throws:
        java.lang.Exception - if there is a problem during the connection establishment.
      • connect

        public void connect​(java.lang.String host,
                            int port,
                            boolean useSSLonCommandChannel,
                            boolean useSSLonDataChannel,
                            boolean useExplicitModeSSL)
                     throws java.lang.Exception
        Creates a connection to the given host and port. The two parameters useSSLonCommandChannel and useSSLonDataChannel determine the SSL settings on the two channels respectively. If useSSLonCommandChannel is true, then the useExplicitModeSSL parameter will determine if the command channel will use explicit or implicit mode SSL.
        Parameters:
        host - the host to connect to.
        port - the port to connect to.
        useSSLonCommandChannel - whether to use SSL on the command channel.
        useSSLonDataChannel - whether to use SSL on the data channel.
        useExplicitModeSSL - whether to negotiate SSL on the control channel (ftpes).
        Throws:
        java.lang.Exception - if there is a problem during the connection establishment.
      • disconnect

        public void disconnect()
        Disconnects the client from the FTP server.
      • login

        public void login​(java.lang.String user,
                          java.lang.String password)
                   throws java.lang.Exception
        Logs to the FTP server using the given credentials.
        Parameters:
        user - the username to be used.
        password - the password to be used.
        Throws:
        java.lang.Exception - if the logging process is unsuccessful.
      • cwd

        public void cwd​(java.lang.String cwd)
                 throws java.lang.Exception
        Changes the working directory with the one specified.
        Parameters:
        cwd - the new working directory.
        Throws:
        java.lang.Exception - if the process fails.
      • setupDataSocket

        public void setupDataSocket()
                             throws java.lang.Exception
        Creates the data socket used for data transferring between the FTP client and server. If the client is in passive mode it creates a Socket and attempts to connect to the server, otherwise it creates a ServerSocket and waits for the server to make the connection. The security settings of the client(whether SSL is used or not) are taken into account when sreating the socket.
        Throws:
        java.lang.Exception - if the operation fails.
      • getFile

        public java.net.Socket getFile​(java.lang.String remoteFile,
                                       boolean binary)
                                throws java.lang.Exception
        Retrieves the specified file from the FTP server.
        Parameters:
        remoteFile - the file to be copied.
        binary - the type of the file (ASCII text or binary data). This determines the transfer mode used by the client.
        Returns:
        a socket from which to read the contents of the file.
        Throws:
        java.lang.Exception - if the transfer fails.
      • getFile

        public void getFile​(java.lang.String remoteFile,
                            java.lang.String localFile,
                            boolean binary)
                     throws java.lang.Exception
        Retrieves the specified file from the FTP server.
        Parameters:
        remoteFile - the file to be copied.
        localFile - the local file in which the contents of the remote to be transfered.
        binary - the type of the file (ASCII text or binary data). This determines the transfer mode used by the client.
        Throws:
        java.lang.Exception - if the transfer fails.
      • putFile

        public java.net.Socket putFile​(java.lang.String remoteFile,
                                       boolean binary)
                                throws java.lang.Exception
        Sends a local file to the server.
        Parameters:
        remoteFile - the file to be put on the server.
        binary - the type of the file (ASCII text or binary data). This determines the transfer mode used by the client.
        Returns:
        the socket used for sending the file contents.
        Throws:
        java.lang.Exception - if the operation fails.
      • putFile

        public void putFile​(java.lang.String localPath,
                            java.lang.String remoteFile,
                            boolean binary)
                     throws java.lang.Exception
        Sends a local file to the server.
        Parameters:
        localPath - the location of the transfered file on the FTP client machine.
        remoteFile - the file to be put on the server.
        binary - the type of the file (ASCII text or binary data). This determines the transfer mode used by the client.
        Throws:
        java.lang.Exception - if the operation fails.
      • setTransferMode

        public void setTransferMode​(boolean binary)
                             throws java.lang.Exception
        Sets the transfer mode according to the type of files to be transfered.
        Parameters:
        binary - if true the files will be sent as binary data, otherwise they will be sent as ASCII text.
        Throws:
        java.lang.Exception - if the operation failed and the server did not return a OK code.
      • deleteFile

        public void deleteFile​(java.lang.String remoteFile)
                        throws java.lang.Exception
        Deletes a given file from the FTP server.
        Parameters:
        remoteFile - the file to be deleted.
        Throws:
        java.lang.Exception - if the operation fails.
      • rename

        public void rename​(java.lang.String from,
                           java.lang.String to)
                    throws java.lang.Exception
        Renames a file or directory on the FTP server.
        Parameters:
        from - name of file or directory to rename.
        to - intended name.
        Throws:
        java.lang.Exception - if the operation fails.
      • pwd

        public java.lang.String pwd()
                             throws java.lang.Exception
        Get the current remote working directory.
        Returns:
        the current working directory on the FTP server.
        Throws:
        java.lang.Exception - if the request fails.
      • mkdir

        public java.lang.String mkdir​(java.lang.String dir)
                               throws java.lang.Exception
        Creates the directory
        Parameters:
        dir - Path of the directory to be created
        Throws:
        java.lang.Exception - if the request fails.
      • sendCommand

        public java.lang.String sendCommand​(java.lang.String cmd)
                                     throws java.lang.Exception
        Sends the given command through the command channel.
        Parameters:
        cmd - the command sent to the FTP server.
        Returns:
        the FTP server response.
        Throws:
        java.lang.Exception - if the operation fails.
      • dir

        public java.lang.String[] dir()
                               throws java.lang.Exception
        Returns a long list of the contents of the current working directory on the FTP server.
        Returns:
        an array of the contents.
        Throws:
        java.lang.Exception
      • list

        public java.lang.String[] list()
                                throws java.lang.Exception
        Returns a short list of the contents of the current working directory on the FTP server.
        Returns:
        the
        Throws:
        java.lang.Exception
      • dir

        public java.lang.String[] dir​(java.lang.String path,
                                      boolean longlisting)
                               throws java.lang.Exception
        Returns a string array with the contents of the given path.
        Parameters:
        path - the path which contents will be listed.
        longlisting - whether the list will be long or short.
        Returns:
        the contents of the path.
        Throws:
        java.lang.Exception
      • checkComplete

        public void checkComplete()
                           throws java.lang.Exception
        Checks whether the file transfer (when getting/putting a file on the FTP server) is over.
        Throws:
        java.lang.Exception - if the operation fails.
      • list

        public java.net.Socket list​(java.lang.String path,
                                    boolean longlisting)
                             throws java.lang.Exception
        Lists the contents of a given path on the FTP server. If the path refers to a file sends information for that file.
        Parameters:
        path - the path which contents must be shown.
        longlisting - determines whether the list should be short or long.
        Returns:
        the socket used for transferring the contents.
        Throws:
        java.lang.Exception - if the operation fails.
      • getResponse

        public java.lang.String getResponse()
                                     throws java.lang.Exception
        Retrieves the server response to a previously sent by the client command.
        Returns:
        the server response.
        Throws:
        java.lang.Exception - if the operation fails.
      • setUsePassive

        public void setUsePassive​(boolean value)
        Sets this FTPClient to use passive mode (or not).
        Parameters:
        value - If true, try to use passive mode before falling back to the old PORT mode. If false, never use passive mode.
      • getUsePassive

        public boolean getUsePassive()
        Returns true if we should try to use passive mode.
        Returns:
        true if passive mode is to be used, otherwise false.