2010-11-23 108 views

回答

2

你可以在你的下载/上传代码中添加一些代码,如例如:

InputStream is = ...; 
long totalBytesRead = 0; 
long bytesRead = 0; 
long startTime = System.currentTimeMillis(); 

while ((bytesRead = is.read(...))!=0) { 
    // Do something useful with the bytes you read 

    totalBytesRead += bytesRead; 
} 

long endTime = System.currentTimeMillis(); 
float dataRate1 = totalBytesRead/(float) (endTime - startTime); // Bytes/Millisecond 
float dataRate2 = dataRate1 * 1000/1024.0f; // kiloBytes/second 

当然,你也可以定期更新你内在的传输速率,同时循环获得的进展过程中的平均增长率。