How to Download File from URL in Java

Downloading file fro the server is a common part of the programming. There are many ways to download Files from URL in Java.

1). Using NIO
2). Using Java input output stream
3). Using Apache Common IO

1). Using NIO

We can use Java NIO Channels or Java IO InputStream to read data from the URL open stream and then save it to file. In this download file from the URL method, we are creating a byte channel from URL stream data. Then use the file output stream to write it to file.

package com.javacodestuffs.files.download; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; public class JavaDownloadFileFromURL { public static void main(String[] args) { String url = "https://github.com/balakoder/SpringMvcAnnotationBased/archive/master.zip"; String fileName = "/home/devuser/master.zip";
try { downloadUsingNIO(url, fileName);
System.out.println("File Downloaded completed using java NIO. "); } catch (IOException e) { e.printStackTrace(); } } private static void downloadUsingNIO(String urlStr, String file) throws IOException { URL url = new URL(urlStr); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(file); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); } } output: File Downloaded completed using java NIO.

2). Using Java Input Output Stream

In this case, we have downloadFile method. we are using the URL openStream method to create the input stream. Then we are using a file output stream to read data from the input stream and write to the file.

package com.javacodestuffs.files.download; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; public class DownloadFileUsingIO { public static void main(String[] args) { String fileUrl = "https://github.com/balakoder/SpringMvcAnnotationBased/archive/master.zip"; String fileName = "/home/devuser/masterio.zip"; try { downloadFile(fileName, fileUrl); System.out.println("File Download completed using Java input output stream. "); } catch (IOException e) { e.printStackTrace(); } } public static void downloadFile(String fileName, String fileUrl) throws MalformedURLException, IOException { BufferedInputStream inStream = null; FileOutputStream outStream = null; try { URL fileUrlObj = new URL(fileUrl); inStream = new BufferedInputStream(fileUrlObj.openStream()); outStream = new FileOutputStream(fileName); byte data[] = new byte[1024]; int count; while ((count = inStream.read(data, 0, 1024)) != -1) { outStream.write(data, 0, count); } } finally { if (inStream != null) inStream.close(); if (outStream != null) outStream.close(); } } } output: File Download completed using Java input output stream.

3). Using apache common IO

This is a pretty simple way. We have to just call a copyURLToFile of FileUtils class. We have to pass parameters as fileUrl and local filename to be saved.

package com.javacodestuffs.files.download; import java.io.File; import java.io.IOException; import java.net.URL; import org.apache.commons.io.FileUtils; public class DownloadFileUsingCommonIO { public static void main(String[] args) { String fileUrl = "https://github.com/balakoder/SpringMvcAnnotationBased/archive/master.zip"; String fileName = "/home/devuser/master2.zip"; downloadFile(fileName, fileUrl); System.out.println("File Downloaded completed using apache Common IO. "); } public static void downloadFile(String fileName, String fileUrl) { try { FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName)); } catch (IOException e) { System.out.println(e.getStackTrace()); } } } output: File Downloaded completed using apache Common IO.

Post/Questions related to How to Download File from URL in Java

How to download a pdf file using java?
Create New File in Java
Read file to byte[] array in Java Java File Reader Examples
Java file writer Example
java.nio.file.Files methods with Examples
Java File Path, Absolute Path and Relative Path
Path toString() method in Java

In this article, we have seen How to download File from URL in Java with Examples. All source code in the article can be found in the GitHub repository.