嗨,我正在使用下载工具,但发现下载速度太慢.我已经检查了下载速度看起来很慢.我的代码如下,
float fileSize = Float.parseFloat(uc.getHeaderField("Content-Length"));
in = new BufferedInputStream(uc.getInputStream());
System.out.println("File size : " + fileSize);
fout = new FileOutputStream(settingsForm.downloadDirectoryText.getText() + File.separatorChar + fileName);
int BUFFER_SIZE = 10240;
byte data[] = new byte[BUFFER_SIZE];
int count = 0;
int totalDownloaded = 0;
long downloadStartTime = System.currentTimeMillis();
long remainingTime = 0;
while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
totalDownloaded += count;
long elapsedTime = System.currentTimeMillis() - downloadStartTime;
float speedInBytes = 1000f * totalDownloaded / elapsedTime;
float tmpSpeed = speedInBytes;
if (tmpSpeed > 1024) {
tmpSpeed = tmpSpeed / 1024;
}
if (tmpSpeed > 1024 * 1024) {
tmpSpeed = tmpSpeed / (1024 * 1024);
}
System.out.println("Speed : " + tmpSpeed);
System.out.println("Remaining Time : " + (fileSize - totalDownloaded) / speedInBytes + " seconds");
int downloadPercentage = (int) ((totalDownloaded / fileSize) * 100);
fout.write(data, 0, count);
}
我发现下载耗时90秒,平均下载速度为60-70kbps.
我已将下载链接指定为Free Download Manager,它将在10秒内下载相同的文件.它能够以超过400kpbs的下载速度下载同一文件.
为什么我不能高速下载?
我的代码有什么问题吗?
提前致谢.