File Download Manager in Java

Download manager is a program that helps us download files from Internet efficiently. The program can pause or resume downloads. It also can split the files to be downloaded into 2 or more segments, which are processed in parallel, making the download faster. In this post, I will share with you how I write a download manager program in Java with those features.

Simple Download Thread
To download a file, we need to create a thread to handle the download so that it will not intercept the GUI thread. Here is the code for run() method of the download thread. It is quite simple.

The BUFFER_SIZE (in bytes) should be multiple of the block size of your hard disk. Usually we use 4096 or 8192. As I comment in the code, firstly we open the connection to the server and get the input stream to read the file. Then we have a while loop to read and write a number of bytes of file to disk.

Parallel Downloading
To download multiple parts of a file parallelly, we need to create multiple threads. Each thread is implemented similarly to the simple thread above, except that it needs to download only a part of the downloaded file. To do that, the HttpURLConnection or its super class URLConnection provides us method setRequestProperty to set the range of the bytes we want to download.

The first thread has the value of mStartByte = 0 while the value of mEndByte of the last thread is the file size (in bytes). When we set the range of the bytes, the server only sends that specific segment of the file, hence the BufferInputStream just read normally. I also think that it is good if the size of each part we download is a multiple of block size of hard disk (which is usually 4092).

Resume/Pause download
To support resume/pause feature, every time the threads read and write to the buffer, we increase the mStartByte variable to the number of bytes we read. So, if the download is paused and resumed later, we can use it to set the range of byte request to the server as above. Here is the full run() function of downloading thread:

Note that, before the InputStream read the file, the output file (type of RandomAccessFile) need to seek to the mStartByte position of the file, then it will start writing at that position.

That’s my simple download manager program. There are more download manager’s features such as scheduling a download, integrating with web browsers, supporting https download etc.

You can download my program at :-)

  • Bob

    This is great help, but sadly it suffers from the same problem as other tutorials do: variable names. “raf”? “in”? This teaches people bad coding habits.

    • Gia Thuy

      Hi Bob,
      Thank you for your comment. Would you like suggest how should I name them? I named them “raf” and “in” as they are abbreviation of long words.

      • vipin

        how to run your rpogramme which class has main() function …

        • vipin

          Plz help me …

  • himansh pal

    Hey i wasnt able to run that download manger. its shows lots of errors. is the proj uploaded on git are with errors..?? or can give me a jar file..?

  • Tulasi

    Hi Great Work… Nice tutorialll!!!!

    • Gia Thuy

      Thank you =)

  • bala sundaram

    I learnt so much from your code. But I could not use your project directly. To use download manager well, we have to copy link address from web pages.If any one uses this project do these:
    In the presume NetBeans for editing)
    1.(design view)add a mouseClicked method for JTextField (jtxURL);
    2. (source view)In the mouseClicked method add this code :
    if (evt.isMetaDown ) jtxURL .paste();
    Now you can copy and past link addresses using right-click on address text field and add to download.

    • Gia Thuy

      Thank you for your comment, Sundaram! =)

  • Long Hoang Giang

    Nice, all best for you, Thuy

  • pratima

    i am doing mca but i want to know the deep knowledge of jsp and servlet and also plz tell me how to make the easy easy application in j2me.i am very much confused in that plz guide me.

  • hiru

    its working. mxxa. Download source code and run. Grrrrrrrrrrrreeeeeet…… :D

  • Dmitii

    What is it for? conn.getResponseCode() / 100 !=2 ?
    Why U just haven`t used conn.getResponseCode()!=200?
    Or it is a noob control :)

  • QooBooS

    Hey, Gia Thuy! :)
    A few days ago I started my journey with Java language. I figured out a lot of things, but still I can’t get used to threads. I thought it would be great to learn from a complete program. I’ve been always wondering, how do download managers work, so I googled and got in here. I decided to modify your code a little bit to see where I can get by myself and got stuck…
    I’d like my program to do something after finishing all the downloads, like shutting down the program or even computer, or simply playing sound, but I can’t find anything in the source code that would notify the main thread that all the downloads are complete. Could you please tell me how do I check whether all the download threads are finished or not, and do something if they are? I think it’s possible to do something like this in the GUI class (downloads are added after clicking the ‘Add’ button), but I’m not sure…

    BTW: Your program works like a charm. It’s simple yet helpful :)


  • James

    This is cool stuff …nice clean and works great !! I upgraded this for ssl with basic auth and it works great with it as well .

    • Gia Thuy

      Hi James, I’m glad to hear that. It will be great if you can share with us your project. Thanks! =]

  • anonymous

    hey, I have multiple network adapters and wanted to know if I can modify the code to create threads and download the files for each physical interfaces. For example a 4mb file, when you download this discharge a portion of an interface and the other by the other interface part

  • Anonymous

    hey, I have multiple network adapters and wanted to know if I can modify the code to create threads and download the files for each physical interfaces. For example a 4mb file, when you download this discharge a portion of an interface(2mb ) and the other by the other (2mb)interface part

    • Gia Thuy

      Hi, you’re free to use the code. But I’m not sure how to do threads in multiple network adapters. If you have a solution for it, can u let me know. Thanks!

      • Anonymous

        ok,I will, im working in that.
        But one more question , how about if i want to integrate it in a browser,like default Downloader Manager.Can you give a idea…

        Thanks2u…Excellent Work!!

  • pooja

    hey Gia, Hello i want to create a web browser with a downloadmanager can u please help me ??????

  • speakakshit

    thanks dude… U helped me for downloading file part by part. But problem in your code is that it won’t download from indirect URLs. I have the code for that.

    • sumith

      if you have download manager code and if it is easy to understand please can you send me to my mail
      Thank you

    • srihar

      can you send me that code too please? my email address is

    • Gia Thuy

      Hi Speakakshit, It will be great if you can share your code with us. Thanks :)

  • Mani

    Hi, great work, Any clue on integerating with web browser .


  • Muhammad Asif

    Can you help me in making a website downloader