Downloading A File In Java Is Easy, But How About Resuming The Download Once It Stopped?
There are many ways to download a file using Java. Either using Java standard libraries, or using external ones. But there are times where we want to not only download the file, but to resume the download in case it stopped for whatever reason. Many of the applications we use, like Internet browsers & download managers already offer that, so why not our Java application? This has many applications. It allows your application to run on poor connection. It’s also more feasible to download large files, as well.
Quickly go to:-
It’s All About Getting Back To Where The Download Stopped, In Bytes
The trick is to resume the download to examine how much of the file we have downloaded so far, and to resume downloading from there. Any new byte we download after that will be appended to the file we already downloaded before. If the file was done downloading, then the algorithm will stop right there.
Here are the lines of code that does that (scroll down for the full code):-
DownloadedSoFar = fileToCheck.length(); urlconnection.setRequestProperty("Range", "bytes=" + DownloadedSoFar + "-"); FOS = new FileOutputStream(fileToCheck, true); outputStream = new BufferedOutputStream(FOS);
If the file doesn’t exist (we never started downloading it), then the code will download it from scratch:-
FOS = new FileOutputStream(fileToCheck); outputStream = new BufferedOutputStream(FOS);
A simple file.exists is all we need to know whether the file exists or not:-
if(fileToCheck.exists())
Here’s the code that does all that. As usual, I wrapped the code for you in a function, so that you could start using it right away:-
public static void DownloadFileWithResume(String FileURL , String FilePath) throws IOException{ File fileToCheck = new File(FilePath); URL url = new URL(FileURL); BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; long DownloadedSoFar = 0; URLConnection urlconnection = url.openConnection(); FileOutputStream FOS = null; if(fileToCheck.exists()){ DownloadedSoFar = fileToCheck.length(); urlconnection.setRequestProperty("Range", "bytes=" + DownloadedSoFar + "-"); FOS = new FileOutputStream(fileToCheck, true); outputStream = new BufferedOutputStream(FOS); }else{ FOS = new FileOutputStream(fileToCheck); outputStream = new BufferedOutputStream(FOS); } urlconnection.connect(); inputStream = new BufferedInputStream(urlconnection.getInputStream()); byte[] buffer = new byte[8192]; int byteCount; while ((byteCount = inputStream.read(buffer)) != -1){ outputStream.write(buffer, 0, byteCount); } // while inputStream.close(); outputStream.flush(); outputStream.close(); }
And Finally
Resuming a file download that already started may seem like a daunting task, but it’s so simple, as all the tools you want are already included with Java itself.
I hope my post has helped you with resuming you Java file downloads, and see you again in another tutorial.
See Also:-
-
- Tech Fairy – Technology From Another World
- What are business
- Java VS Python VS C# detailed comparison, which language to learn first? – Tech Fairy
- Do you need an Expensive motherboard for gaming? – Tech Fairy
- What is the meaning of the different USB port colors? (blue, teal blue, yellow, red, black)
- Why motherboards & laptops still come with USB 2.0 ports When USB 3.0 Is Backward Compatible?
- USB 2.0 VS USB 3.0 Comparison: What are the differences between the two ports?
- History Of Graphics card motherboard slots: PCI VS AGP VS PCI-Express VS Integrated graphics.
- What are the advantages & disadvantages of using a Trackball? And why you should use one
- What’s the difference between remake & remaster? (With multiple examples).