Java URL Download download(URL url, File file)

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

Description

Downloads from the given url to the file without spamming the log.

License

Open Source License

Parameter

Parameter Description
url a parameter
file a parameter

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code


//package com.java2s;
/*//w  w w. ja  va 2  s . c  o  m
 * Copyright (C) 2011 Moritz Schmale <narrow.m@gmail.com>
 *
 * NarrowtuxLib 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.logging.Logger;

public class Main {
    /**
     * Downloads from the given url to the file.
     * @param log
     * @param url
     * @param file
     * @throws IOException
     */
    public static void download(Logger log, URL url, File file) throws IOException {
        boolean hasLog = log != null;
        if (!file.getParentFile().exists())
            file.getParentFile().mkdir();
        if (file.exists())
            file.delete();
        file.createNewFile();
        final int size = url.openConnection().getContentLength();
        if (hasLog)
            log.info("Downloading " + file.getName() + " (" + size / 1024 + "kb) ...");
        final InputStream in = url.openStream();
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        final byte[] buffer = new byte[1024];
        int len, downloaded = 0, msgs = 0;
        final long start = System.currentTimeMillis();
        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
            downloaded += len;
            if ((int) ((System.currentTimeMillis() - start) / 500) > msgs) {
                if (hasLog)
                    log.info((int) ((double) downloaded / (double) size * 100d) + "%");
                msgs++;
            }
        }
        in.close();
        out.close();
        if (hasLog)
            log.info("Download finished");
    }

    /**
     * Downloads from the given url to the file without spamming the log.
     * @param url
     * @param file
     * @throws IOException
     */
    public static void download(URL url, File file) throws IOException {
        download(null, url, file);
    }
}

Related

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