Java URL Download nio downloadFromInternet(URL url, File downloadTo)

Here you can find the source of downloadFromInternet(URL url, File downloadTo)

Description

download From Internet

License

Open Source License

Declaration

public static void downloadFromInternet(URL url, File downloadTo) throws IOException 

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.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class Main {
    public static void downloadFromInternet(URL url, File downloadTo) throws IOException {
        ReadableByteChannel rbc = null;
        FileOutputStream fos = null;
        try {/*w w w.j  a  va 2 s.  c om*/
            rbc = Channels.newChannel(url.openStream());
            fos = new FileOutputStream(downloadTo);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        } finally {
            closeQuietly(rbc);
            closeQuietly(fos);
        }
    }

    /**
     * Close a stream quietly because we honestly don't care if a stream.close()
     * throws IOException
     */
    public static void closeQuietly(Closeable cl) {
        if (cl == null) {
            return;
        }
        try {
            cl.close();
        } catch (IOException ioe) {
            // do nothing
        }
    }
}

Related

  1. downloadFile(URL url, File targetFile)
  2. downloadFileNIO(FileChannel fileChannel, SocketChannel socketChannel)
  3. downloadFileToDirectory(String url, File destination)
  4. downloadFirstLineFromInternetQuietly(URL url)
  5. downloadFromHttpUrl(String destPkgUrl, FileOutputStream outputStream)
  6. downloadImage(String src, Path saveFolder)
  7. downloadToFile(String filename, String urlString)
  8. downloadToFile(URL url, File file)
  9. downloadToString(String url)