2

I'm developing an application on Android that connects to a FTP server to upload and download files. To make the connection I'm using the apache commons-net library's FTPClient class based on this and I'm working on Eclipse.

But I get the following message on my Logcat:

07-04 21:11:44.196: D/USB Virtual(14708): Error: could not connect to host ftp://xxx.xxx

The following is my manifest permissions:

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

I'm really lost as to why I can't connect to my FTP server, in the console there're no errors showing. Am I missing something? I would thank any help.

I'm adding the code I'm using to connect to my FTP server:

public boolean ftpConnect(String host, String username, String password,
        int port) {
    try {
        mFTPClient = new FTPClient();
                    //host is ftp://looner-project.zxq.net
        mFTPClient.connect(host);
        mFTPClient.connect(InetAddress.getByName(host));


        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {

            boolean status = mFTPClient.login(username, password);
            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;
}
Uriel
  • 1,365
  • 2
  • 12
  • 15

1 Answers1

0

Things to check...

1. Check your internet connection.

2. Are you giving the Url to the ftp site correct ??

3. If its password protected, are you giving them correctly ??

4. Using command prompt check it. Give the below command to check it.

ftp www.your_ftp_site.com

after the above command you will be prompted for username and password.

I am also attaching my code which i used to upload audio file to the server to make it clear...

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.IOException;

    import org.apache.commons.net.ftp.FTP;

    import org.apache.commons.net.ftp.FTPClient;

    import android.util.Log;



    public class MyOwn {

        public void goforIt(){


            FTPClient con = null;

            try
            {
                con = new FTPClient();
                con.connect("www.mysamle.net");                 // Its dummy Address

                if (con.login("uju495", "Stevejobs!!"))
                {
                    con.enterLocalPassiveMode();                   // Very Important

                    con.setFileType(FTP.BINARY_FILE_TYPE);        //  Very Important
                    String data = "/sdcard/Vivekm4a.m4a";

                    FileInputStream in = new FileInputStream(new File(data));
                    boolean result = con.storeFile("/Ads/Vivekm4a.m4a", in);
                    in.close();
                    if (result) Log.v("upload result", "succeeded");
                    con.logout();
                    con.disconnect();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }   
        }

    }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • my code is almost like yours but the connection isn't happening, I'm really lost here. – Uriel Jul 05 '12 at 03:18
  • @Uriel: connect method has several overloads; maybe try with InetAddress parameter. See also [here](http://stackoverflow.com/questions/5719449/creating-inetaddress-object-in-java/5719553#5719553) – Erwin Nov 20 '13 at 14:04