Java Utililty Methods URL Download nio

List of utility methods to do URL Download nio

Description

The list of methods to do URL Download nio are organized into topic(s).

Method

voiddownloadFile(String url, String localFilePath)
download File
try {
    URL website = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(localFilePath);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
} catch (java.net.MalformedURLException e) {
} catch (java.io.FileNotFoundException e) {
...
booleandownloadFile(String url, String location)
download File
try {
    final URLConnection connection = new URL(url).openConnection();
    connection.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0");
    final int contentLength = connection.getContentLength();
    final File destination = new File(location);
    if (destination.exists()) {
        final URLConnection savedFileConnection = destination.toURI().toURL().openConnection();
...
booleandownloadFile(String url, String location)
download File
try {
    final URLConnection connection = createURLConnection(url);
    final int contentLength = connection.getContentLength();
    final File destination = new File(location);
    if (destination.exists()) {
        final URLConnection savedFileConnection = destination.toURI().toURL().openConnection();
        if (savedFileConnection.getContentLength() == contentLength) {
            return true;
...
FiledownloadFile(String urlPath, String local)
download File
File tempFile = new File(local);
tempFile.createNewFile();
URL url = new URL(urlPath);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(local);
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
fos.close();
return tempFile;
...
voiddownloadFile(URL downloadUrl, File destination)
download File
System.out.println("Downloading file from URL: " + downloadUrl + " to destination: " + destination);
Files.copy(downloadUrl.openStream(), destination.toPath());
FiledownloadFile(URL url, File output)
Download a file
output.getParentFile().mkdirs();
downloadingFile = output;
URLConnection connection = url.openConnection();
downloadingFileSize = connection.getContentLengthLong();
ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
FileOutputStream fos = new FileOutputStream(output);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
...
voiddownloadFile(URL url, File targetFile)
download File
FileOutputStream fos = new FileOutputStream(targetFile);
url = getUrlFollowingRedirects(url);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
voiddownloadFileNIO(FileChannel fileChannel, SocketChannel socketChannel)
download File NIO
long pos = 0;
long fileSize = fileChannel.size();
fileChannel.transferFrom(socketChannel, pos, fileSize);
voiddownloadFileToDirectory(String url, File destination)
download File To Directory
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
@SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream(destination.getAbsolutePath());
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
StringdownloadFirstLineFromInternetQuietly(URL url)
download First Line From Internet Quietly
BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(url.openStream(), Charset.forName("UTF-8")));
    return reader.readLine();
} catch (IOException ioe) {
    return "";
} finally {
    closeQuietly(reader);
...