Java URL Download download(URL source, File destination)

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

Description

Downloads the file which is avaiable under the given source URL to the given destination File .

License

Open Source License

Parameter

Parameter Description
source The source URL where you can find the file which should be downloaded.
destination The destination File where the downloaded file should be stored.

Exception

Parameter Description
IOException Something goes wrong while opening a connection, reading the stream or executing some file operations.

Declaration

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

Method Source Code


//package com.java2s;
/*//from ww w  . j av  a2s .  co  m
 * This file is part of QuarterBukkit-Plugin.
 * Copyright (c) 2012 QuarterCode <http://www.quartercode.com/>
 *
 * QuarterBukkit-Plugin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QuarterBukkit-Plugin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QuarterBukkit-Plugin. If not, see <http://www.gnu.org/licenses/>.
 */

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 {
    /**
     * Downloads the file which is avaiable under the given source {@link URL} to the given destination {@link File}.
     * 
     * @param source The source {@link URL} where you can find the file which should be downloaded.
     * @param destination The destination {@link File} where the downloaded file should be stored.
     * @throws IOException Something goes wrong while opening a connection, reading the stream or executing some file operations.
     */
    public static void download(URL source, File destination) throws IOException {

        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = source.openStream();
            outputStream = new FileOutputStream(destination);
            outputStream.flush();

            byte[] tempBuffer = new byte[4096];
            int counter;
            while ((counter = inputStream.read(tempBuffer)) > 0) {
                outputStream.write(tempBuffer, 0, counter);
                outputStream.flush();
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }
    }
}

Related

  1. download(String url, String saveFile)
  2. download(String url, String toFile)
  3. download(String urlAddress, File file)
  4. download(String urlString)
  5. download(String urlString, File output)
  6. download(URL sourceUrl, File destinationFile)
  7. download(URL url)
  8. download(URL url, File destination)
  9. download(URL url, File file)