3

こんにちは、ダウンロード ツールに取り組んでいますが、ダウンロード速度が遅すぎることがわかりました。ダウンロード速度が遅いように見えることを確認しました。私のコードは次のとおりです。

        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 秒以内にダウンロードするFree Download Managerへのダウンロード リンクを提供しました。400kpbs 以上のダウンロード速度で同じファイルをダウンロードできます。

そんなに速くダウンロードできないのはなぜですか?

私のコードに何か問題がありますか?

前もって感謝します。

4

1 に答える 1

3

Even without modification your code should run a lot faster than 60 KB/s. You can add a BufferedWriter but the difference is negligible. You use the faster method to copy : read/write with a byte buffer. BufferedReader add speed when you use the read() method (which reads character per character). However the size of the buffer can improve the process, I have good results with 64 * 1024.

You can also remove your if (tmpSpeed > 1024 ... and replace with (no 'if' is needed here) :

double tmpSpeed = speedInBytes / 1024;

I have tested our program on my machine from one HD to another and I got the following speed with a file of 1GB :

21057ms
49797 KB/s  >>> 60 KB/s

It is not your code the problem, the slowness come from the network. Try with different files from different servers, etc ...

于 2013-04-24T08:29:17.917 に答える