java 如何在下载过程中显示有关带宽速度的用户友好格式输出
想要在下载期间显示有关带宽速度的格式良好的输出,
感谢@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 语句中放入什么?
希望你明白我想做什么
Want to show nicely formatted output regarding bandwidth speed during download
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)
}
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);
}
My problem is what do i put in the if statement?.
hope you understand what i try to do
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个
FileUtils
Apache Commons 中的 .byteCountToDisplaySize()
方法IO:另请参阅(可能重复):
There is a
FileUtils.byteCountToDisplaySize()
method in Apache Commons IO:Also see (possible duplicates):
我真的不明白你到底想要什么,令人困惑的是你的交换机每秒有兆字节。
正如您似乎意识到的那样, switch 语句需要具有枚举或整数 - 而您当前的数字两者都不是。
如果您想随着数字变大而自动从 Kbit/s 移动到 Mbit/s,那么我认为您需要使用带有阈值的 if 语句。
如果您想采用用户设置的设置作为首选项,那么您只需将该设置(枚举或整数)传递到此函数中,以便它可以以所需的格式处理答案。
如果您不想做这两件事,那么我发现您的问题有点令人困惑。
I really don't understand exactly what you want it is confusing that you have mega**bytes per second in your switch.
As you seem to be aware a switch statement needs to have either an enum or an int - and that you current number is neither.
If you want to automatically move from Kbit/s to Mbit/s as the number gets larger then I think you want to use an if statement with a threshold.
If you want to take a setting that the user sets as a preference then you just need to pass that setting (either an enum or an int) into this function so that it can process the answer in the required format.
If you want to do neither of these things then I find your question a little confusing.