【问题标题】:Download Video file from a FTP server in Android在 Android 中从 FTP 服务器下载视频文件
【发布时间】:2023-06-21 11:57:01
【问题描述】:

我正在开发一个 Android 应用程序来连接到 FTP 服务器并下载托管在 FTP 服务器上的视频文件并保存到 SD 卡内的文件夹中。为此,我正在使用“org.apache.commons.net.ftp.FTPClient”库。我可以连接到服务器并从我的应用程序中获取服务器上的文件名列表,但是在我下载视频文件后,它们会被复制到我提供的文件夹中,并且大小显示为 10 个字节并且无法播放(看起来像损坏了)。有人可以建议我解决这个问题的方法吗?我使用的代码附在此处。

** 连接到 FTP 服务器**

public FTPClient mFTPClient = null;

public boolean ftpConnect(String host, String username,String password, int port)
{
    try {
        mFTPClient = new FTPClient();
        mFTPClient.connect(host,port);

        // now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) 
        {
            // login using username & password
            boolean status = mFTPClient.login(username, password);

            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

            return status;
        }
    } 
    catch(Exception e) 
    {
        System.out.println("Error: could not connect to host " + host);
        e.printStackTrace();
    }

    return false;
}

下载文件

public boolean ftpDownload(String srcFilePath, String desFilePath)
{
    boolean status = false;
    try {
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);

        status = mFTPClient.retrieveFile(srcFilePath, desFileStream);

        desFileStream.close();

        return status;
    } catch (Exception e) {
        System.out.println( "download failed");
        e.printStackTrace();
    }

    return status;
}

【问题讨论】:

    标签: android video ftp download client


    【解决方案1】:

    retrieveFile 也给我带来了问题。尝试使用retrieveFileStream。

    【讨论】: