Check out my iOS apps here
As an Amazon Associate I earn from qualifying purchases.

How to download & resume file download using Java

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.

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

Leave a Comment

Your email address will not be published. Required fields are marked *