【问题标题】:How to download a file from FTP server to Android device?如何将文件从 FTP 服务器下载到 Android 设备?
【发布时间】:2014-08-27 15:24:40
【问题描述】:

我需要将图像从 FTP 服务器下载到 Android 设备。我使用 ftp4j-1.7.2.jar 库尝试了几个示例,但无法连接 FTP 服务器并且搞砸了。

有人使用过 FTP 服务器吗?

请建议从服务器建立连接和下载文件。

【问题讨论】:

    标签: android ftp


    【解决方案1】:

    使用库commons.apache.org/proper/commons-net

    检查以下代码以从 FTP 服务器下载文件:

    private Boolean downloadAndSaveFile(String server, int portNumber,
        String user, String password, String filename, File localFile)
        throws IOException {
    FTPClient ftp = null;
    
    try {
        ftp = new FTPClient();
        ftp.connect(server, portNumber);
        Log.d(LOG_TAG, "Connected. Reply: " + ftp.getReplyString());
    
        ftp.login(user, password);
        Log.d(LOG_TAG, "Logged in");
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        Log.d(LOG_TAG, "Downloading");
        ftp.enterLocalPassiveMode();
    
        OutputStream outputStream = null;
        boolean success = false;
        try {
            outputStream = new BufferedOutputStream(new FileOutputStream(
                    localFile));
            success = ftp.retrieveFile(filename, outputStream);
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    
        return success;
    } finally {
        if (ftp != null) {
            ftp.logout();
            ftp.disconnect();
        }
    }
    }
    

    【讨论】:

    • 嗨,我在我的桌面上安装了 FileZilla FTP 服务器。它的地址是 127.0.0.1,端口是 14147。我必须为 UserName、Password、fileName、localfile 提供什么??
    • 使用此链接lifehacker.com/339887/build-a-home-ftp-server-with-filezilla 使用filezilla 创建用户并了解如何使用它。或检查此设置是否相同addictivetips.com/windows-tips/…
    • 你用的是什么库???我在 org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:471) 收到 java.lang.NullPointerException
    猜你喜欢
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多