Press enter to see results or esc to cancel.


JAVA-Download a file from URL

How to download a file from URL using Java.
The jar file org.apache.commons.io.jar should be added to class path as shown in the video

Update the Directory Name, File Name and URL as per your need.

1. Download_URL.java

package URL_Download;
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 org.apache.commons.io.FileUtils;

public class Download_URL {
    public static void main(String[] args) {
        // Make sure that this directory exists
        String dirName = "E:\\downloaded";
        try {
            saveFileFromUrlWithJavaIO(
                dirName + "\\java_tutorial.png", "http://www.j2eebrain.com/wp-content/uploads/c11e504954111c54b467c154d4abc2d3.png");
            System.out.println("finished");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // Using Java IO
    public static void saveFileFromUrlWithJavaIO(String fileName, String fileUrl)
    throws MalformedURLException, IOException {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try { in = new BufferedInputStream(new URL(fileUrl).openStream());
            fout = new FileOutputStream(fileName);
            byte data[] = new byte[1024];
            int count;
            while ((count = in .read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } finally {
            if ( in != null)
                in .close();
            if (fout != null)
                fout.close();
        }
    }
    // Using Commons IO library
    // Available at http://commons.apache.org/io/download_io.cgi
    public static void saveFileFromUrlWithCommonsIO(String fileName,
        String fileUrl) throws MalformedURLException, IOException {
        FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));
    }
}

Comments

Leave a Comment