2015-06-24 40 views
1

几天前,我已经提出了用于测量下载速度的代码。不过,我不太明白URL & outputfile中要求的代码的工作方式。有人可以向我解释吗?我只对Java有一个非常基本的理解。提前致谢。使用Eclipse测量下载速度

public class Speed { 
    @SuppressWarnings("resource") 
    public double getSpeed() throws IOException{ 
     URL website = new URL("https://www.youtube.com/"); //The source website 
     ReadableByteChannel rbc = Channels.newChannel(website.openStream()); 
     File outputFile = new File("output.jpg"); //The output file 
     outputFile.createNewFile(); 
     FileOutputStream fos = new FileOutputStream(outputFile); 
     long startTime = System.nanoTime(); //Measure when you start to download the file, we know that the time it takes to download a file is endTime-startTime 
     fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 

     long endTime = System.nanoTime(); //Measure when we're done downloading the file 
     long fileBytes = outputFile.length(); 
     double downloadTimeSeconds = ((double)(endTime-startTime))/1000000000; //1 billion nanoseconds in a second 
     double bytesPerSecond = ((double)fileBytes)/downloadTimeSeconds; 
     return bytesPerSecond;   
    } 
} 

我已经测试与download.html的代码文件输出& http://www.thinkbroadband.com/download.html的URL。然而,它仅仅是3KB之间返回值 - 300KB不管我下载一个文件或不...

+2

难道你不明白这部分的代码? – Mudassar

+0

下载**你应该测量什么**? –

+0

为什么不尝试阅读文档?你的问题(_什么是必须的.​​.....)表明你在尝试时遇到了问题。你是否?哪一个? – PJTraill

回答

1

URL - >上要下载

输出文件网页 - >它将在哪里下载。按照代码它将在您的项目文件夹中创建。不知道他们为什么以.jpg命名。你可以任意命名它..打开该文件,你会得到很多下载的html。

什么返回 - >按该变量的名称,显然按照逻辑,它是“每秒下载字节”

+0

你是什么意思“打开该文件,你会得到很多下载的html”。 – Kevin

+0

将扩展名为txt的文件命名。然后在一些文本编辑器(如记事本)中打开该输出文件(该文件将在您运行程序后创建)。您将在程序中看到您使用该网址的网页的html代码。 –