几天前,我已经提出了用于测量下载速度的代码。不过,我不太明白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不管我下载一个文件或不...
难道你不明白这部分的代码? – Mudassar
下载**你应该测量什么**? –
为什么不尝试阅读文档?你的问题(_什么是必须的......)表明你在尝试时遇到了问题。你是否?哪一个? – PJTraill