Java URL Download nio downloadUrl(String urlstring, File file)

Here you can find the source of downloadUrl(String urlstring, File file)

Description

download Url

License

Apache License

Declaration

public static boolean downloadUrl(String urlstring, File file) 

Method Source Code


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

import java.io.*;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class Main {
    public static boolean downloadUrl(String urlstring, File file) {
        try {/*  w  w  w .  j a  va  2s .  c o  m*/
            URL url = new URL(urlstring);
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            FileOutputStream fos = new FileOutputStream(file);
            fos.getChannel().transferFrom(rbc, 0, 1 << 24);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean downloadUrl(String urlstring, String filename) {
        return downloadUrl(urlstring, new File(filename));
    }
}

Related

  1. downloadFromInternet(URL url, File downloadTo)
  2. downloadImage(String src, Path saveFolder)
  3. downloadToFile(String filename, String urlString)
  4. downloadToFile(URL url, File file)
  5. downloadToString(String url)