我正在从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;
} …Run Code Online (Sandbox Code Playgroud)