Java多连接下载

发布于 2024-08-21 06:11:48 字数 197 浏览 5 评论 0原文

我想得到一些建议,我已经开始一个新项目来创建一个将使用多个连接的 java 下载加速器。我想知道如何最好地解决这个问题。

到目前为止,我已经发现我可以使用 HttpUrlConnection 并使用 range 属性,但想知道一种有效的方法。一旦我从多个连接下载了各个部分,我就必须加入这些部分,以便我们最终得到一个完全下载的文件。

提前致谢 :)

I wanted to get some advice, I have started on a new project to create a java download accelerator that will use multiple connections. I wanted to know how best to go about this.

So far I have figured out that i can use HttpUrlConnection and use the range property, but wanted to know an efficient way of doing this. Once i have download the parts from the multiple connections i will then have to join the parts so that we end up with a fully downloaded file.

Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裂开嘴轻声笑有多痛 2024-08-28 06:11:48
  1. 获取要下载的文件的内容长度
  2. 根据标准(大小、速度……)划分
  3. 运行多个线程从不同位置开始下载文件,
    并将它们保存在不同的文件中:myfile.part1myfile.part2...
  4. 下载后,将各个部分合并到一个文件中。

我尝试使用以下代码来获取内容长度:

public Downloader(String path) throws IOException {
    int len = 0;
    URL url = new URL(path);
    URLConnection connectUrl = url.openConnection();
    System.out.println(len = connectUrl.getContentLength());
    System.out.println(connectUrl.getContentType());

    InputStream input = connectUrl.getInputStream();
    int i = len;
    int c = 0;
    System.out.println("=== Content ==="); 
    while (((c = input.read()) != -1) && (--i > 0)) {
        System.out.print((char) c);
    }
    input.close(); 
}

这是加入文件的示例代码:

public void join(String FilePath) {
    long leninfile=0, leng=0;
    int count=1, data=0;
    try {
        File filename = new File(FilePath);
        RandomAccessFile outfile = new RandomAccessFile(filename,"rw");
        while(true) {
            filename = new File(FilePath + count + ".sp");
            if (filename.exists()) {
                RandomAccessFile infile = new RandomAccessFile(filename,"r");
                data=infile.read();
                while(data != -1) {
                    outfile.write(data);
                    data=infile.read();
                }
                leng++;
                infile.close();
                count++;
            } else break;
        }
        outfile.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
}
  1. Get the content length of the file to download.
  2. Divide it according to a criteria (size, speed, …).
  3. Run multiple threads to download the file starting at different positions,
    and save them in different files: myfile.part1,  myfile.part2, …
  4. Once downloaded, join the parts into one single file.

I tried the following code to get the content length:

public Downloader(String path) throws IOException {
    int len = 0;
    URL url = new URL(path);
    URLConnection connectUrl = url.openConnection();
    System.out.println(len = connectUrl.getContentLength());
    System.out.println(connectUrl.getContentType());

    InputStream input = connectUrl.getInputStream();
    int i = len;
    int c = 0;
    System.out.println("=== Content ==="); 
    while (((c = input.read()) != -1) && (--i > 0)) {
        System.out.print((char) c);
    }
    input.close(); 
}

Here's a sample code to join the files:

public void join(String FilePath) {
    long leninfile=0, leng=0;
    int count=1, data=0;
    try {
        File filename = new File(FilePath);
        RandomAccessFile outfile = new RandomAccessFile(filename,"rw");
        while(true) {
            filename = new File(FilePath + count + ".sp");
            if (filename.exists()) {
                RandomAccessFile infile = new RandomAccessFile(filename,"r");
                data=infile.read();
                while(data != -1) {
                    outfile.write(data);
                    data=infile.read();
                }
                leng++;
                infile.close();
                count++;
            } else break;
        }
        outfile.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
}
两相知 2024-08-28 06:11:48

如果您想避免在下载后加入段,可以使用 文件通道
使用 FileChannel< /a>,您可以写入文件的任何位置(即使有多个线程)。

因此,您可以首先分配整个文件,然后
将片段写在它们所属的位置在它们出现时

有关详细信息,请参阅 Javadocs 页面

If you want to avoid joining segments after downloading you could use a FileChannel.
With a FileChannel, you can write to any position of a file (even with multiple threads).

So you could first allocate the whole file, and then
write the segments where they belong as they come in.

See the Javadocs page for more info.

天荒地未老 2024-08-28 06:11:48

JDownloader 是我见过的最好的下载器。如果您感兴趣,它是开源的,您当然可以从他们的代码中学到很多东西。

JDownloader is the best downloader I've seen. If you are interested, it's open source and surely you can learn a lot from their code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文