【问题标题】:Android FTP, cannot connect and needs advice on import libraryAndroid FTP,无法连接,需要关于导入库的建议
【发布时间】:2013-03-05 08:38:59
【问题描述】:

对不起大家,我一整天都在添加FTP,但没有成功.....

以下是我如何将 FTP 添加到我的 android 代码的步骤。

方法A来自laval@work

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

从链接中,我下载了一个 zip 文件夹: Commons Net 3.2(需要 Java 1.5 或更高版本)>>源代码>>commons-net-3.2-src.zip

2) 解压文件夹到我的桌面,commons-net-3.2-src

3) 将文件夹 src/main/java/org 复制到我的代码的 src 文件夹中

4) 允许在清单中使用权限“INTERNET”

5) 将以下代码添加到 src/MainActivity(我的应用程序源代码)中

6) ftp 码:来自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) 在 OnCreate 内部,添加

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

方法B来自here

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

从链接中,下载了一个 zip 文件夹: Commons Net 3.2(需要 Java 1.5 或更高版本)>>二进制文件>>commons-net-3.2-bin.zip

2) 解压文件夹到我的桌面,commons-net-3.2-bin

3) 将两个java文件commons-net-3.2和commons-net-3.2-sources复制到android app的libs文件中

4) 打开应用程序的“属性”对话框,导航到“Java Build Path”->“Libraries”并添加对两个 jar 的引用。 导航到“Java Build Path”->“Order and Export”并选择导出这两个jar。

5) 同方法 A,步骤 4 - 7

但是,我仍然无法连接到 FTP 服务器.... =[=[=[=[ 因为 LogCat 仍然显示错误消息“错误:无法连接到主机 [Ljava.lang.StackTraceElement;@420c3228null "

我还在我的标签上下载了一个 FTP 应用程序 (ftpcafe),并证明用户名和位置设置正常...

=[ 拜托,有人可以帮忙吗?谢谢!!

【问题讨论】:

    标签: android ftp ftp-client


    【解决方案1】:

    试试这个代码。

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

    【讨论】:

    • SimpleFTP?那是我需要导入的库吗?!
    • 我目前仅使用此代码连接我的 ftp 服务器和上传文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2011-12-11
    • 1970-01-01
    • 2017-04-16
    • 2016-08-31
    相关资源
    最近更新 更多