How to download file from URL in java

In this post, we will see how to download file from URL in java. It can be used when you want to automatically download any file from URL using java.

There are many ways to do it and some of them are :

Java Program:

package org.arpit.java2blog;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import org.apache.commons.io.FileUtils;

/*
 * @author Arpit Mandliya
 */
public class FileDownloadFromURLMain {

 public static void main(String[] args) {

  String dirName = "/Users/Arpit/Desktop/Blog";

  try {

   System.out.println("---------------------------");
   System.out.println("Downloading file from github using java file IO");

   // Using java IO
   downloadFileFromUrlWithJavaIO(
     dirName + "/sampleFile1.zip",
     "https://github.com/arpitmandliya/SpringRestfulWebServicesWithJSONExample/archive/master.zip");

   System.out.println("Downloaded file from github using java file IO");
   System.out.println("---------------------------");
   System.out.println("Downloading file from github using apache common IO");

   // Using Apache common IO
   downloadFileFromUrlWithCommonsIO(
     dirName + "/sampleFile2.zip",
     "https://github.com/arpitmandliya/SpringSecurityHelloWorldExample/archive/master.zip");

   System.out.println("Downloaded file from github using apache common IO");
   System.out.println("---------------------------");
   System.out.println("Downloading file from github using NIO");

   // Using NIO
   downloadFileFromURLUsingNIO(
     dirName + "/sampleFile3.zip",
     "https://github.com/arpitmandliya/SpringMVCHelloWorldExample/archive/master.zip");

   System.out.println("Downloaded file from github using NIO");
   System.out.println("---------------------------");

  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 // Using Java IO
 public static void downloadFileFromUrlWithJavaIO(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();
  }
 }

 // Using common IO
 public static void downloadFileFromUrlWithCommonsIO(String fileName,
   String fileUrl) throws MalformedURLException, IOException {
  FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));
 }

 // Using NIO
 private static void downloadFileFromURLUsingNIO(String fileName,String fileUrl) throws IOException {
  URL url = new URL(fileUrl);
  ReadableByteChannel rbc = Channels.newChannel(url.openStream());
  FileOutputStream fOutStream = new FileOutputStream(fileName);
  fOutStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  fOutStream.close();
  rbc.close();
 }

}

When you run above program , file will be downloaded in above mentioned directory.You will get following output:

---------------------------
Downloading file from github using java file IO
Downloaded file from github using java file IO
---------------------------
Downloading file from github using apache common IO
Downloaded file from github using apache common IO
---------------------------
Downloading file from github using NIO
Downloaded file from github using NIO
---------------------------

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.