想要在下载过程中显示有关带宽速度的格式良好的输出
感谢@Tomasz Nurkiewicz,我在下面进行了计算,它显示了
下载文件时每秒兆*字节*。
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 speed = NANOS_PER_SECOND / BYTES_PER_MIB * totalRead / (System.nanoTime() - start + 1)
}
希望是这样的。我每秒从
计算,然后我输入一个 if 语句
来选择
KByte/s、MBit/s(不确定)或就像普通的 FTP 客户端显示速度一样。
if( KByte/s something) {
System.out.print(your current speed xx KB/s);
}else if(MByte/s something){
System.out.print(your current speed xx MB/s);
}
我的问题是我应该在 if 语句中放入什么?
希望你明白我想做什么
最佳答案
有一个FileUtils.byteCountToDisplaySize()
Apache Commons IO中的方法:
System.out.println(
"your current speed is " + FileUtils.byteCountToDisplaySize(12345) + "/s"
)
// your current speed is 12 KB/s
另请参阅(可能重复):
关于java 如何在下载过程中显示有关带宽速度的用户友好格式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8157437/