Java multiple connection downloading

我的未来我决定 提交于 2019-11-30 05:03:36
Syed M Shaaf
  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();
    }
}
zockman

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!