当前位置: 代码迷 >> J2SE >> 怎样计算下载速度?解决方案
  详细解决方案

怎样计算下载速度?解决方案

热度:6   发布时间:2016-04-24 13:00:41.0
怎样计算下载速度??
Java code
            URL url = new URL(urls);            URLConnection conn = url.openConnection();            conn.connect();            System.out.println(urls + " 长度:" + conn.getContentLength() / 1024 + "KB");            InputStream fis = conn.getInputStream();            long a = System.currentTimeMillis();            FileOutputStream out = new FileOutputStream(path);            byte buf[] = new byte[10240];            int n;            while ((n = fis.read(buf)) != -1) {                out.write(buf, 0, n);            }            fis.close();            out.close();            long b = System.currentTimeMillis();            System.out.println("[+]: 下载费时:"+ String.valueOf(b-a) + "毫秒");


1:怎样得到 每秒下载速度 ?
2:n 是每次读取的字节数吗?

------解决方案--------------------
设个标记,没读一次流就加1

在开个timer线程,每隔一秒钟读一次标记,请将标记置0

标记数*10240(你每次读流的字节数)就是下载速度...
  相关解决方案