Reputation: 33
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.
Upvotes: 3
Views: 4896
Reputation: 1
So, before calling the downloading function retrieveFile
add a line like this:
mFTPClient.setBufferSize(1024*1024);
This is the correct solution. My app was slow to download 10 files in 20 minutes. With this modification of buffer takes 1 minute. Genial. Thank you very much.
Upvotes: 0
Reputation: 799
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);
Upvotes: 11