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 :
- Using Java input output stream
- Using apache common IO
- Using NIO
Java Program:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
--------------------------- 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 --------------------------- |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.