如何将文件从 FTP 服务器下载到 Android 设备? [英] How to download a file from FTP server to Android device?

查看:32
本文介绍了如何将文件从 FTP 服务器下载到 Android 设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.jar library, but failed to connect with FTP servers & messed up.

有人使用过 FTP 服务器吗?

Have anyone worked with FTP servers?

请建议建立连接&从服务器下载文件.

Please suggest to make connection & download file from Server.

推荐答案

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

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

Check below code to download file from FTP server:

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

这篇关于如何将文件从 FTP 服务器下载到 Android 设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆