Java URL Download nio download(File dist, URL src)

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

Description

download

License

Open Source License

Declaration

public static boolean download(File dist, URL src) 

Method Source Code

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

import java.io.Closeable;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

public class Main {
    public static boolean download(File dist, URL src) {

        InputStream is = null;//from   www.j a v  a 2  s  . c  o  m
        ReadableByteChannel in = null;
        FileOutputStream fos = null;
        FileChannel out = null;
        try {

            URLConnection connection = src.openConnection();
            is = connection.getInputStream();
            in = Channels.newChannel(is);

            fos = new FileOutputStream(dist);
            out = fos.getChannel();

            out.transferFrom(in, 0, connection.getContentLength());

            return true;

        } catch (IOException e) {

            return false;

        } finally {
            close(in, is, out, fos);
        }

    }

    public static void close(Closeable... closeables) {

        for (Closeable c : closeables)
            try {
                c.close();
            } catch (IOException e) {
                /* ignore */ }

    }
}

Related

  1. canDownload(String filePath)
  2. download(String from, String to)
  3. download(String link, File destFile)
  4. download(String url, File output)
  5. download(String url, String destPath)