读书人

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

发布时间: 2011-12-31 23:50:30 作者: rapoo

怎样计算下载速度??

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(你每次读流的字节数)就是下载速度...

读书人网 >J2SE开发

热点推荐