0
votes

Sorry everybody, I've been adding FTP whole day with no success.....

Here are the steps how I add FTP to my android code.

Method A from laval@work

1) http://commons.apache.org/proper/commons-net//download_net.cgi

From the link, I downloaded one of the zip folders: Commons Net 3.2 (Requires Java 1.5 or later)>>Source>>commons-net-3.2-src.zip

2) Extract the folder to my Desktop, commons-net-3.2-src

3) Copy folder src/main/java/org into my code's src folder

4) Allow uses permission "INTERNET" in the manifest

5) Add the below code into src/MainActivity (that my source code for the app)

6) The ftp code: from the android community

public boolean ftpConnect(String host, String username, String password,
        int port) {
    try {
        ftp = new FTPClient();
        // connecting to the host
        ftp.connect(host, port);

        // now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            // login using username & password
            Log.d(TAG, "connection success" + host);
            boolean status = ftp.login(username, password);

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();

            return status;
        }
    } catch (Exception e) {
        Log.e(TAG, "Error: could not connect to host "
                + e.getStackTrace().toString() + e.getMessage());
    }

    return false;
}

7) inside OnCreate, add

ftpConnect("192.168.1.71", "user", "password", 21);

Method B from here

1) http://commons.apache.org/proper/commons-net//download_net.cgi

From the link, downloaded one of the zip folders: Commons Net 3.2 (Requires Java 1.5 or later)>>Binaries>>commons-net-3.2-bin.zip

2) Extract the folder to my Desktop, commons-net-3.2-bin

3) Copy two java files, commons-net-3.2 and commons-net-3.2-sources into android app's libs file

4) Open the app’s Properties dialog, navigate to “Java Build Path”->”Libraries” and add the reference to the two jars. Navigate to “Java Build Path”->”Order and Export” and select to export the two jars.

5) Same as method A, step 4 - 7

HOWEVER, I still failed to connect to the FTP server.... =[=[=[=[ as the LogCat still shows error message "Error: could not connect to host [Ljava.lang.StackTraceElement;@420c3228null "

I have also downloaded a FTP app (ftpcafe) onto my tab and proofed that the user name and locations settings are working...

=[ Please, can anybody help? thanks!!

1

1 Answers

0
votes

Have a try with this code.

SimpleFTP ftp = new SimpleFTP();

         try
         {
            // Connect to an FTP server on port 21.

            ftp.connect("host", 21, "username", "password");

            // Set binary mode.
            ftp.bin();

            // Change to a new working directory on the FTP server.


            ftp.cwd("/httpdocs/yourdestinationfolderinftp");

            // Upload some files.
            ftp.stor(new File("/mnt/sdcard/ftp.jpg"));              

            // Quit from the FTP server.
            ftp.disconnect();

         }
         catch (Exception e) {
            // TODO: handle exception
             e.printStackTrace();
        }


    }