Java URL Download downloadFromUrl(URL url, String localFilename)

Here you can find the source of downloadFromUrl(URL url, String localFilename)

Description

Downloads a file from a URL

License

Open Source License

Parameter

Parameter Description
url The URL to download from
localFilename The local file name

Exception

Parameter Description
IOException If an error occured.

Declaration

public static void downloadFromUrl(URL url, String localFilename) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *     DataDude is a data managing application designed to have many types of data in one application
 *     Copyright (C) 2015  Ahmed R. (theTechnoKid)
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

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

import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**//from w  w  w . j  ava  2s.  c  om
     * Downloads a file from a URL
     * @param url The URL to download from
     * @param localFilename The local file name
     * @throws IOException If an error occured.
     */
    public static void downloadFromUrl(URL url, String localFilename) throws IOException {
        InputStream is = null;
        FileOutputStream fos = null;

        try {
            URLConnection urlConn = url.openConnection();// connect

            is = urlConn.getInputStream(); // get connection inputstream
            fos = new FileOutputStream(localFilename); // open outputstream to
            // local file

            byte[] buffer = new byte[4096]; // declare 4KB buffer
            int len;

            // while we have availble data, continue downloading and storing to
            // local file
            while ((len = is.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
        } finally {
            if (is != null) {
                is.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }
}

Related

  1. downloadFileFromWebserver(String fileUrl, String storageLocation)
  2. downloadFileIfAvailable(URL url, File destination)
  3. downloadFileToGivenNameElseExtension( URLConnection urlConnection, String fileName)
  4. downloadFromURL(String url)
  5. downloadFromUrl(String urlString, PrintStream logger)
  6. downloadGzipCompressedFile(URL url, File destination)
  7. downloadHttp(String downloadURL, String basePath, String commonPathURL)
  8. downloadImg(String urlPath, File file)
  9. downloadMobiFromEpubUrl(String href)