13

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 :)

2
  • 1
    Your question is not terribly clear or specific. Could you possibly clarify. Thanks Feb 11, 2010 at 8:31
  • @ all: Questions need votes too! Oct 18, 2011 at 15:27

3 Answers 3

11
  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();
    }
}
2
  • 1
    thanks Shaaf i think that is just what i was looking for, will give it a bash :) Feb 11, 2010 at 10:55
  • I am failing to see in your sample method Downloader (horrible to use a method name starting with upper-case character, BTW) how you do the multi-stream downloading. I would have expected to see you spawning multiple threads, starting downloads at different offsets etc. I wonder why this answer was even accepted, because other than theoretically, it does not address the question of multi-stream downloading.
    – kriegaex
    May 6, 2021 at 7:26
8

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.

1
  • 1
    +1: I think FileChannel is the most efficient/easy way to do this. Oct 18, 2011 at 15:29
2

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.

1
  • ...surely you can learn a lot from their code... not only that, you can even modify it or use it on your own program. (it's open-source!) Oct 18, 2011 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.