测量下载速度Java

时间:2011-06-12 15:30:27

标签: java file download measure

我正在努力下载一个软件上的文件,这是我得到的,它可以下载,并且我可以获得进步,但还有一件事我不知道该怎么做。测量下载速度。我很感激你的帮助。谢谢。 这是当前的下载方法代码

    public void run()
    {
        OutputStream out = null;
        URLConnection conn = null;
        InputStream in = null;
        try
        {
            URL url1 = new URL(url);
            out = new BufferedOutputStream(
            new FileOutputStream(sysDir+"\\"+where));
            conn = url1.openConnection();
            in = conn.getInputStream();
            byte[] buffer = new byte[1024];
            int numRead;
            long numWritten = 0;
            double progress1;
            while ((numRead = in.read(buffer)) != -1)
            {
                out.write(buffer, 0, numRead);
                numWritten += numRead;
                this.speed= (int) (((double)
                buffer.length)/8);
                progress1 = (double) numWritten;
                this.progress=(int) progress1;
            }
        }
        catch (Exception ex)
        {
            echo("Unknown Error: " + ex);
        }
        finally
        {
            try
            {
                if (in != null)
                {
                    in.close();
                }
                if (out != null)
                {
                    out.close();
                }
            }
            catch (IOException ex)
            {
                echo("Unknown Error: " + ex);
            }
        }
    }

3 个答案:

答案 0 :(得分:8)

与测量任何东西的方式相同。

System.nanoTime()会返回Long,您可以用它来衡量某事需要多长时间:

Long start = System.nanoTime();
// do your read
Long end = System.nanoTime();

现在你有读取X字节所需的纳秒数。算一算,你有下载率。

您很可能每秒都在寻找字节数。跟踪您已读取的总字节数,检查是否已经过了一秒。一秒钟后,根据您在该时间内读取的字节数计算出速率。重置总数,重复。

答案 1 :(得分:2)

这是我的实现

while (mStatus == DownloadStatus.DOWNLOADING) {
            /*
             * Size buffer according to how much of the file is left to
             * download.
             */
            byte buffer[];
            // handled resume case.
            if ((mSize < mDownloaded ? mSize : mSize - mDownloaded <= 0 ? mSize : mSize - mDownloaded) > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[(int) (mSize - mDownloaded)];
            }

            // Read from server into buffer.
            int read = stream.read(buffer);
            if (read == -1)
                break;// EOF, break while loop

            // Write buffer to file.
            file.write(buffer, 0, read);
            mDownloaded += read;
            double speedInKBps = 0.0D;
            try {
                long timeInSecs = (System.currentTimeMillis() - startTime) / 1000; //converting millis to seconds as 1000m in 1 second
                speedInKBps = (mDownloaded / timeInSecs) / 1024D;
            } catch (ArithmeticException ae) {

            }
            this.mListener.publishProgress(this.getProgress(), this.getTotalSize(), speedInKBps);
        }

答案 2 :(得分:1)

我可以给你一个大致的想法。在下载开始时启动计时器。现在,将(percentage downloaded)乘以download size,然后除以time elapsed.,即可获得平均下载时间。希望我能让你走上正轨!

您可以按照Brian的建议使用System.nanoTime();

long startTime = System.nanoTime();放在while循环之外。和

long estimatedTime = System.nanoTime() - startTime;将为您提供循环中经过的时间。