Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FTP Download in Android Extremely Slow (Fast in just Java)

I am downloading MP3 files from an FTP server. It is for an Android application which will download and the then play the MP3 files. The downloading is implemented in Java using apache commons library and the code is largely based on another tutorial. The download works very fast on my desktop running Java taking about 5 seconds to download a file which is ~10mb, but on the same code run on an Android device (I have tried 2) is ridiculously slower taking 5-10 minutes to download the same file. (Both tests were done over Wifi).

Code based on: http://androiddev.orkitra.com/?p=28&cpage=2#comment-40

The code below shows the two methods used: connect and download.

    public boolean connect(String host, String username, String pass, int port){
    try{

        mFTPClient.connect(host, port);

        if(FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                boolean loginStatus = mFTPClient.login(username,  pass);

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

                return loginStatus;
        }


    } catch (Exception e){
        System.err.println("Error: Could not connect to: " + host);
        e.printStackTrace();
    }

    return false;
}

    public boolean download(String srcFilePath, String dstFilePath) {
    boolean downloadStatus = false;
    try {
        FileOutputStream dstFileStream = new FileOutputStream(dstFilePath);
        downloadStatus = mFTPClient.retrieveFile(srcFilePath,   dstFileStream);
        dstFileStream.close();
        return downloadStatus;
    } catch (Exception e) {
        System.err.println("Error: Failed to download file from " + srcFilePath + " to " + dstFilePath);
    }
    return downloadStatus;
}

Hopefully I have mentioned all the details needed and would appreciate if anyone could explain why it is so much slower and how if at all I can make it download in a reasonable time.

like image 940
Simon M Avatar asked May 26 '26 12:05

Simon M


1 Answers

Stumbled accross a similar issue, solved it by changing the download buffer size.

What was strange is the same code was very fast on Android emulator x86, but painfully slow on the real device.

So, before calling the downloading function retrieveFile add a line like this:

mFTPClient.setBufferSize(1024*1024);
like image 183
Arise Avatar answered May 30 '26 05:05

Arise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!