简体   繁体   English

Android下载速度非常慢(仅Java快速下载)

[英]FTP Download in Android Extremely Slow (Fast in just Java)

I am downloading MP3 files from an FTP server. 我正在从FTP服务器下载MP3文件。 It is for an Android application which will download and the then play the MP3 files. 这是一个Android应用程序,它将下载然后播放MP3文件。 The downloading is implemented in Java using apache commons library and the code is largely based on another tutorial. 下载是使用apache commons库在Java中实现的,代码主要基于另一个教程。 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. 下载在我运行Java的桌面上运行速度非常快,大约需要5秒才能下载一个大约10mb的文件,但是在Android设备上运行相同的代码(我已经尝试过2次),下载时需要5-10分钟才会慢得多同一个文件。 (Both tests were done over Wifi). (两项测试均通过Wifi完成)。

Code based on: http://androiddev.orkitra.com/?p=28&cpage=2#comment-40 代码基于: 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. 希望我已经提到了所需的所有细节,如果有人能解释为什么它如此慢,以及如何在合理的时间内下载它,我将不胜感激。

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. 奇怪的是,相同的代码在Android模拟器x86上非常快,但在真实设备上却很慢。

So, before calling the downloading function retrieveFile add a line like this: 所以,在调用下载函数retrieveFile之前添加一行如下:

mFTPClient.setBufferSize(1024*1024);

So, before calling the downloading function retrieveFile add a line like this: 所以,在调用下载函数retrieveFile添加一行如下:

mFTPClient.setBufferSize(1024*1024);

This is the correct solution. 这是正确的解决方案。 My app was slow to download 10 files in 20 minutes. 我的应用程序在20分钟内下载10个文件的速度很慢。 With this modification of buffer takes 1 minute. 用这个缓冲区修改需要1分钟。 Genial. 和煦。 Thank you very much. 非常感谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM