• Android Help With implementing ftp to Android app

    MarkGrillo Member

    i am in need of help with finding a good background ftp client code that can send a file or folder to a ftp server, i have researched for some time and found the following code below but i am not sure how to get it to run in the background.
    Basically in my application i have a button and i want to setup the button to send a file to my ftp server which will be hard coded into the code and run in the background. I know i need to use and Async task method but i am not very familiar with async task method or how to implement it to a button listener. so any help or advise will be greatly appreciated. Below is the source code i am working with and having problem implementing.

    Method to connect to FTP server:

        public boolean ftpConnect(String host, String username,
                                  String password, int port)
        {
            try {
                mFTPClient = new FTPClient();
                // connecting to the host
                mFTPClient.connect(host, port);
    
                // now check the reply code, if positive mean connection success
                if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                    // login using username & password
                    boolean status = mFTPClient.login(username, password);
    
                    /* Set File Transfer Mode
                     *
                     * To avoid corruption issue you must specified a correct
                     * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
                     * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
                     * for transferring text, image, and compressed files.
                     */
                    mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                    mFTPClient.enterLocalPassiveMode();
    
                    return status;
                }
            } catch(Exception e) {
                Log.d(TAG, "Error: could not connect to host " + host );
            }
    
            return false;
        }
    

    Method to change working directory:

    public boolean ftpChangeDirectory(String directory_path)
    {
        try {
            return mFTPClient.changeWorkingDirectory(directory_path);
        } catch(Exception e) {
            Log.d(TAG, "Error: could not change directory to " + directory_path);
        }
    
        return false;
    

    Method to upload a file to FTP server:

    /**
     * mFTPClient: FTP client connection object (see FTP connection example)
     * srcFilePath: source file path in sdcard
     * desFileName: file name to be stored in FTP server
     * desDirectory: directory path where the file should be upload to
     */
    public boolean ftpUpload(String srcFilePath, String desFileName,
                             String desDirectory)
    {
        boolean status = false;
        try {
            FileInputStream srcFileStream = new FileInputStream(srcFilePath);
    
            // change working directory to the destination directory
            if (ftpChangeDirectory(desDirectory)) {
                status = mFTPClient.storeFile(desFileName, srcFileStream);
            }
    
            srcFileStream.close();
            return status;
        } catch (Exception e) {
            Log.d(TAG, "upload failed");
        }
    
        return status;
    }
    
Viewing 0 reply threads
  • You must be logged in to reply to this topic.