在几个部分或段中下载java中的文件

时间:2018-06-01 23:10:46

标签: java multithreading concurrency download

我试图以多段方式在java中下载文件(即,将它分成几个部分并在一个单独的线程中并行下载每个部分)但是当我使用下面的代码时,似乎每个线程正在下载整个文件,而不仅仅是它的一部分,但是当它完成时,文件正确下载。

请注意" downloadedSizeCombined"是所有线程和ArrayList" downloadedSize"下载的所有字节的总和。跟踪由单个线程下载的字节。

此方法在类下载中,它扩展了SwingWorker。

public Void doInBackground() {
    ExecutorService es = Executors.newCachedThreadPool();
    for (int i = 0; i < MAX_NUMBER_OF_PARTS; i++) {
        int numOfThePart = i;
        es.execute(new Runnable() {
            @Override
            public void run() {
                RandomAccessFile file = null;
                InputStream stream = null;

                try {
                    while (Download.this.getStatus() == WAITINGLIST) {
                        Thread.sleep(1);
                    }
                    // Open connection to URL.
                    HttpURLConnection connection =
                        (HttpURLConnection) url.openConnection();

                    // Specify what portion of file to download.
                    int startByte = numOfThePart * sizeOfFile / MAX_NUMBER_OF_PARTS;
                    int endByte = ((numOfThePart + 1) * sizeOfFile / MAX_NUMBER_OF_PARTS) - 1;
                    if (numOfThePart == MAX_NUMBER_OF_PARTS)
                        endByte = ((numOfThePart + 1) * sizeOfFile / MAX_NUMBER_OF_PARTS);
                    connection.setRequestProperty("Range",
                        "bytes=" + ((startByte + downloadedSize.get(numOfThePart))) + "-" + endByte);

                    // Connect to server.
                    connection.connect();

                    // Check for valid content length.
                    int contentLength = connection.getContentLength();
                    if (contentLength < 1) {
                        System.out.println("1");
                    }

                    /* Set the size for this download if it
                       hasn't been already set. */
                    if (sizeOfFile == -1) {
                        sizeOfFile = contentLength;
                    }

                    file = new RandomAccessFile(new File(s.getCurrentDirectory(), getFileName(url)),
                        "rw");
                    file.seek(startByte + downloadedSize.get(numOfThePart));

                    fileLocation = new File(s.getCurrentDirectory(), getFileName(url));

                    stream = connection.getInputStream();
                    while (status == CURRENT) {
                        file.seek(startByte + downloadedSize.get(numOfThePart));

                        byte buffer[];

                        buffer = new byte[MAX_BUFFER_SIZE];

                        // Read from server into buffer.
                        int read = stream.read(buffer);

                        if (read == -1)
                            break;

                        // Write buffer to file.
                        file.write(buffer, 0, read);
                        downloadedSizeCombined += read;
                        downloadedSize.set(numOfThePart, downloadedSize.get(numOfThePart) + read);

                        publish(numOfThePart);
                        while (status == PAUSED) {
                            Thread.sleep(1);
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    // Close file.
                    if (file != null) {
                        try {
                            file.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    // Close connection to server.
                    if (stream != null) {
                        try {
                            stream.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
    return null;
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

我们不能使用UDP连接吗?因此,如果我们使用DatagramSocket类,它将始终以数据包的形式发送数据。试试这个。 很快就会回来......