Saturday 16 February 2019

Apache commons io: Download a file from url


Below application download a file from server.

App.java
package com.sample.app;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class App {
 public static void getPDF(String sourceURL, String filePathToStore) throws IOException {
     URL url = new URL(sourceURL);
     File file = new File(filePathToStore);
     FileUtils.copyURLToFile(url, file);
 }
 
 public static void main(String args[]) throws IOException {
  getPDF("https://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf", "test.pdf");

 }
}

I used below maven dependency.

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
         <groupId>commons-io</groupId>
         <artifactId>commons-io</artifactId>
         <version>2.6</version>
</dependency>


No comments:

Post a Comment