gpt4 book ai didi

java while循环下载时如何计算Mbit/s

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:54 24 4
gpt4 key购买 nike

我有这个 while loop 下载文件

while ((val = bis.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, val);
fileSize -= val;
if (fileSize < 1024) {
val = (int) fileSize;
}

尝试弄清楚如何像许多速度网站那样显示 Mbit/s。(链接)
http://www.speedtest.net/.

希望测量值留在 while 循环中,但我有
看到使用 on-minute-threads 触发的例子,但我不知道..

不知道数据量,还是数据一直是1024。
这就是我认为的问题
有什么帮助吗?

最佳答案

我可能错了一两个数量级,大致思路如下:

long start = System.nanoTime();
long totalRead = 0;
final double NANOS_PER_SECOND = 1000000000.0;
final double BYTES_PER_MIB = 1024 * 1024;

while ((val = bis.read(buffer, 0, 1024)) > 0) {
//...
totalRead += val;
double speedInMBps = NANOS_PER_SECOND / BYTES_PER_MIB * totalRead / (System.nanoTime() - start + 1);
double speedInMbps = speed * 8;
}

请注意,这计算的是从一开始的平均速度,而不是当前速度。

关于java while循环下载时如何计算Mbit/s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8155672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com