Download a file from a URL in Java
This post will discuss how to download a file from a URL in Java.
There are several ways to download a file from a URL in Java. This post provides an overview of some of the available alternatives to accomplish this.
1. Using FileChannel.transferFrom()
method
java.nio.channels.FileChannel
class in Java provides several methods for reading, writing, mapping, and manipulating a file. It is transferFrom() method transfers bytes into this channel’s file from the given readable byte channel. It accepts three parameters – the source channel, the position within the file at which the transfer is to begin, and the maximum number of bytes to be transferred.
The complete usage is demonstrated below with Java 7 try-with-resource, which take care of closing the opened streams and channels:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; class Main { public static void downloadFile(URL url, String outputFileName) throws IOException { try (InputStream in = url.openStream(); ReadableByteChannel rbc = Channels.newChannel(in); FileOutputStream fos = new FileOutputStream(outputFileName)) { fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } } public static void main(String[] args) throws Exception { // call to `downloadFile()` method } } |
2. Using Files.copy()
method
From Java 7 onward, we can use the java.nio.file.Files.copy() method to copy all bytes from an input stream to a file. It accepts the input stream to read from and the path to the file. Additionally, we can specify how the copying should be done. This is demonstrated below using the try-with-resource block:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; class Main { public static void downloadFile(URL url, String fileName) throws Exception { try (InputStream in = url.openStream()) { Files.copy(in, Paths.get(fileName)); } } public static void main(String[] args) throws Exception { // call to `downloadFile()` method } } |
3. Plain Java
In plain Java, we can read the file byte by byte from the input stream and write the bytes to a file output stream. This would translate to a simple code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.IOException; import java.net.URL; class Main { public static void downloadFile(URL url, String fileName) throws IOException { try (InputStream in = url.openStream(); BufferedInputStream bis = new BufferedInputStream(in); FileOutputStream fos = new FileOutputStream(fileName)) { byte[] data = new byte[1024]; int count; while ((count = bis.read(data, 0, 1024)) != -1) { fos.write(data, 0, count); } } } public static void main(String[] args) throws Exception { // call to `downloadFile()` method } } |
4. Using Apache Commons IO
We can also use Apache Commons IO library, whose FileUtils
class offers handy file manipulation utilities. FileUtils’s copyURLToFile() method can be used to copy bytes from the URL source to the specified file destination. It is recommended to use its overloaded version with connection and read timeout parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; class Main { public static void downloadFile(URL url, String fileName) throws IOException { FileUtils.copyURLToFile(url, new File(fileName)); } public static void main(String[] args) throws Exception { // call to `downloadFile()` method } } |
That’s all about downloading a file from a URL in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)