使用BufferedOutputStream时,Android FTP上传速度非常慢

时间:2013-03-22 01:51:47

标签: android performance

我正在尝试使用FTP构建Android应用程序来执行上传速度测试,但使用以下代码时,上传速度不会高于1 Mbps。在同一设备上使用FTP客户端时,我可以获得15 Mbps的上传速度。有人可以给我一个帮助吗?

public class MultiUL extends Thread{

private String id="hhhhh0001";
private int bufferSize=32768;

public void run() {
    FTPClient ftp = new FTPClient();
    try {       
        ftp.connect(MPSharkVar.Server,21); // Using port no=21
        ftp.login(MPSharkVar.FTPUser, MPSharkVar.FTPPwd);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }     
    uploadWork(ftp, id);           
    try {
        ftp.disconnect();
    } 
    catch (Exception ex) 
    {    
    }
}

private void uploadWork(FTPClient ftp, String id) {

    byte[] buffer = new byte[bufferSize];
    for (int i = 0; i < bufferSize; i++) {
        buffer[i] = 'F';
    }
    OutputStream os = null;
    BufferedOutputStream bos = null;
    long start = System.currentTimeMillis();
    long end = start;
    try {
        ftp.changeWorkingDirectory("/upload");
        os = ftp.storeFileStream(id);
        bos = new BufferedOutputStream(os);
        start = System.currentTimeMillis();
        end = start;
        while (((end - start) <= MPSharkVar.TestDur)) {
            bos.write(buffer, 0, buffer.length);

            end = System.currentTimeMillis();

        }
        } catch (Exception ex) {
            end = System.currentTimeMillis();
        } finally {

                try {
                    bos.flush();
                    bos.close();
                    os.close();      
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

        }
    }

}

0 个答案:

没有答案