如何将文件从 FTP 服务器下载到 Android 设备?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24605622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:29:07  来源:igfitidea点击:

How to download a file from FTP server to Android device?

androidftp

提问by Anil kumar

I have a requirement to download a image from FTP server to android device. I tried with few samples by using ftp4j-1.7.2.jarlibrary, but failed to connect with FTP servers& messed up.

我需要将图像从 FTP 服务器下载到 android 设备。我通过使用ftp4j-1.7.2.jar库尝试了几个样本,但无法与 FTP 服务器连接并搞砸了。

Have anyone worked with FTP servers?

有人用过 FTP 服务器吗?

Please suggest to make connection & download file from Server.

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

回答by Anil Jadhav

Use library commons.apache.org/proper/commons-net

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

Check below code to download file from FTP server:

检查以下代码以从 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();
    }
}
}