Java URL Download download(URL url, File destination)

Here you can find the source of download(URL url, File destination)

Description

download

License

Open Source License

Declaration

public static void download(URL url, File destination) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static void download(URL url, File destination) throws IOException {
        byte[] buffer = new byte[4096];
        int read, total = 0;

        URLConnection conn = url.openConnection();
        conn.connect();// w  ww.j a  v  a2s. c  o m
        InputStream is = conn.getInputStream();
        FileOutputStream out = new FileOutputStream(destination);

        while ((read = is.read(buffer)) > 0) {
            out.write(buffer, 0, read);
            total += read;
        }

        if (total < conn.getContentLength())
            throw new IOException("Download truncated");

        is.close();
        out.close();
    }
}

Related

  1. download(String urlString)
  2. download(String urlString, File output)
  3. download(URL source, File destination)
  4. download(URL sourceUrl, File destinationFile)
  5. download(URL url)
  6. download(URL url, File file)
  7. downloadAndExtract(String fileURL, String targetDirectory)
  8. downloadAndSaveImage(URL imageUrl, String path)
  9. downloadAndUnzip(String url, File location)