幾天前,我已經提出了用於測量下載速度的代碼。不過,我不太明白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