2

我正在 Android 上开发一个连接到 FTP 服务器以上传和下载文件的应用程序。为了建立连接,我正在使用基于的 apache commons-net 库的 FTPClient 类,并且我正在使用 Eclipse。

但我在我的 Logcat 上收到以下消息:

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

以下是我的清单权限:

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

我真的很困惑为什么我无法连接到我的 FTP 服务器,在控制台中没有显示错误。我错过了什么吗?我会感谢任何帮助。

我正在添加我用来连接到我的 FTP 服务器的代码:

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;
}
4

1 回答 1

0

要检查的东西...

1.检查您的互联网连接。

2.你给ftp站点的Url正确吗??

3.如果它的密码保护,你给他们正确吗?

4.使用命令提示符检查它。给出以下命令来检查它。

ftp www.your_ftp_site.com

执行上述命令后,系统将提示您输入用户名和密码。

我还附上了我用来将音频文件上传到服务器的代码以使其清楚......

    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();
            }   
        }

    }
于 2012-07-05T02:53:53.880 回答