java如何在下载期间显示用户友好的格式化输出关于带宽速度 [英] java how to show user friendly formatted output regarding bandwidth speed during download
问题描述
想要在下载期间显示带宽速度的格式良好的输出
Want to show nicely formatted output regarding bandwidth speed during download
下面我有这个计算,谢谢@Tomasz Nurkiewicz,它显示
当我下载一个文件时,每秒的兆字节 *兆字节。
I have this calculation below thanks to @Tomasz Nurkiewicz, and it show
mega*bytes* per second when i download a file.
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)
}
希望像这样。我从
计算得到mega * 字节 *,从那里我输入一个 if语句
在
上选择KByte / s,MBit / s(不确定)或就像普通FTP客户端显示速度一样。
Would like it to be like this. I get mega*bytes* per second from the
calculation and from that i enter a if statement
to select on
KByte/s, MBit/s (not sure) or just like a normal FTP client show speed.
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语句中添加什么?
My problem is what do i put in the if statement?.
希望你明白我想做什么
推荐答案
Apache Commons IO 中的FileUtils.byteCountToDisplaySize()
方法:
There is a FileUtils.byteCountToDisplaySize()
method in Apache Commons IO:
System.out.println(
"your current speed is " + FileUtils.byteCountToDisplaySize(12345) + "/s"
)
// your current speed is 12 KB/s
另请参阅(可能的重复):
Also see (possible duplicates):
- How to convert byte size into human readable format in java?
- Format file size as MB, GB etc
这篇关于java如何在下载期间显示用户友好的格式化输出关于带宽速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!