Java URL Download downloadToFile(URL source, File dest)

Here you can find the source of downloadToFile(URL source, File dest)

Description

Retrieves a file from a web server and writes it to disk.

License

Apache License

Parameter

Parameter Description
source the URL to download
dest the File to write to

Exception

Parameter Description
IOException if the file cannot be downloaded/written

Declaration

public static void downloadToFile(URL source, File dest) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class Main {
    /**//  w w  w .ja v a2  s  . c  om
     * Retrieves a file from a web server and writes it to disk. Supports binary files.
     * 
     * @param source
     *        the {@link URL} to download
     * @param dest
     *        the {@link File} to write to
     * @throws IOException
     *         if the file cannot be downloaded/written
     * @author RisingDemon
     */
    public static void downloadToFile(URL source, File dest) throws IOException {
        final InputStream input = source.openStream();
        byte[] buffer = new byte[4096];
        int n = -1;
        final OutputStream output = new FileOutputStream(dest);
        while ((n = input.read(buffer)) != -1) {
            output.write(buffer, 0, n);
        }
        input.close();
        output.close();
    }
}

Related

  1. downloadText(String url)
  2. downloadTextFromUrl(String url)
  3. downloadTo(String url, String filename)
  4. downloadToDirectory(URL url, String directoryName)
  5. downloadToFile(String url, File file)
  6. downloadToFile(URL url, File downloadFile)
  7. downloadToFile(URL url, File file)
  8. downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)
  9. downloadToString(String url)