Java URL Download downloadFileFromInternet(String remoteUrl)

Here you can find the source of downloadFileFromInternet(String remoteUrl)

Description

download File From Internet

License

Apache License

Declaration

public static File downloadFileFromInternet(String remoteUrl) throws Exception 

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.URL;

public class Main {
    public static File downloadFileFromInternet(String remoteUrl) throws Exception {
        BufferedInputStream inputStream = null;
        BufferedOutputStream outputStream = null;
        try {/*www  .  j  a  v  a  2s .c  om*/
            URL url = new URL(remoteUrl);
            File tempFile = File.createTempFile("temp", "zip");

            inputStream = new BufferedInputStream(url.openStream());
            outputStream = new BufferedOutputStream(new FileOutputStream(tempFile));

            int read = 0;
            while ((read = inputStream.read()) != -1) {
                outputStream.write(read);
            }

            inputStream.close();
            outputStream.flush();
            outputStream.close();
            return tempFile;
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (Exception ex) {

            }
        }
    }
}

Related

  1. downloadFile(URL theURL, String filePath)
  2. downloadFile(URL url, File toFile)
  3. downloadFile(URL url, String name)
  4. downloadFile(URL url, String path, String fileName)
  5. downloadFileAndRetry(final File file, final URL url, final int retry)
  6. downloadFileFromUrl(String urlPath, File file)
  7. downloadFileFromWeb(URL resourceURL, String fullPath)
  8. downloadFileFromWebserver(String fileUrl, String storageLocation)
  9. downloadFileIfAvailable(URL url, File destination)