嗨,我正在开发一个下载工具,我发现下载速度太慢了。我已经检查过下载速度看起来很慢。我的代码如下,
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。
我已将下载链接提供给免费下载管理器,该管理器可在 10 秒内下载相同的文件。它可以以超过 400kpbs 的下载速度下载相同的文件。
为什么我不能以这么快的速度下载?
我的代码有什么问题吗?
提前致谢。