Sim*_*n M 2 java apache android ftp-client apache-commons
我正在从FTP服务器下载MP3文件.这是一个Android应用程序,它将下载然后播放MP3文件.下载是使用apache commons库在Java中实现的,代码主要基于另一个教程.下载在我运行Java的桌面上运行速度非常快,大约需要5秒才能下载一个大约10mb的文件,但是在Android设备上运行相同的代码(我已经尝试过2次),下载时需要5-10分钟才会慢得多同一个文件.(两项测试均通过Wifi完成).
代码基于:http://androiddev.orkitra.com/?p = 28&cpage = 2#comment-40
下面的代码显示了使用的两种方法:连接和下载.
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;
}
Run Code Online (Sandbox Code Playgroud)
希望我已经提到了所需的所有细节,如果有人能解释为什么它会如此慢以及如何在合理的时间内下载它,我将不胜感激.
Ari*_*ise 10
遇到类似问题,通过更改下载缓冲区大小解决了这个问题.
奇怪的是,相同的代码在Android模拟器x86上非常快,但在真实设备上却很慢.
所以,在调用下载函数retrieveFile之前 添加一行如下:
mFTPClient.setBufferSize(1024*1024);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3878 次 |
| 最近记录: |