Java URL Download downloadFile(String url, String path)

Here you can find the source of downloadFile(String url, String path)

Description

download File

License

Open Source License

Declaration

public static void downloadFile(String url, String path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.net.URL;

import java.net.MalformedURLException;
import java.io.BufferedInputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;

public class Main {
    public static void downloadFile(String url, String path) {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {/*from   w w w .j a  v a  2s  .  c  o m*/
            in = new BufferedInputStream(new URL(url).openStream());
            fout = new FileOutputStream(path);

            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } catch (MalformedURLException e) {
            System.err.println("An error occured while downloading a file (1).");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("An error occured while downloading a file (2).");
            e.printStackTrace();
        } finally {
            // Close file IO's
            try {
                if (in != null) {
                    in.close();
                }
                if (fout != null) {
                    fout.close();
                }
            } catch (IOException e) {
                // We can't do anything.
                System.err.println("A critical error occured while downoading a file (3).");
                e.printStackTrace();
                return;
            }
        }
    }
}

Related

  1. downloadFile(String url, File output)
  2. downloadFile(String url, String fileName)
  3. downloadFile(String url, String fileName)
  4. downloadFile(String url, String fileName)
  5. downloadFile(String url, String filePath, File parent)
  6. downloadFile(String urlPath, File file)
  7. downloadFile(String urlString)
  8. downloadFile(String urlString, String filename)
  9. downloadFile(String urlToDownload, File locationToStore)