Java URL Download downloadFileFromUrl(String urlPath, File file)

Here you can find the source of downloadFileFromUrl(String urlPath, File file)

Description

download File From Url

License

Apache License

Declaration

public static long downloadFileFromUrl(String urlPath, File file) 

Method Source Code

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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {

    public static long downloadFileFromUrl(String urlPath, File file) {
        long size = 0;
        try {//from w ww  . j ava 2  s  . c  o m
            URL url = new URL(urlPath);
            HttpURLConnection httpurlconnection = (HttpURLConnection) url
                    .openConnection();
            BufferedInputStream bufferedinputstream = new BufferedInputStream(
                    httpurlconnection.getInputStream());
            BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
                    new FileOutputStream(file));
            int i;
            while ((i = bufferedinputstream.read()) != -1) {
                bufferedoutputstream.write(i);
            }
            bufferedinputstream.close();
            bufferedoutputstream.close();
            httpurlconnection.disconnect();
            size = file.length();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }
}

Related

  1. downloadFile(URL url, File toFile)
  2. downloadFile(URL url, String name)
  3. downloadFile(URL url, String path, String fileName)
  4. downloadFileAndRetry(final File file, final URL url, final int retry)
  5. downloadFileFromInternet(String remoteUrl)
  6. downloadFileFromWeb(URL resourceURL, String fullPath)
  7. downloadFileFromWebserver(String fileUrl, String storageLocation)
  8. downloadFileIfAvailable(URL url, File destination)
  9. downloadFileToGivenNameElseExtension( URLConnection urlConnection, String fileName)