Java URL Download downloadFromUrl(String urlString, PrintStream logger)

Here you can find the source of downloadFromUrl(String urlString, PrintStream logger)

Description

download From Url

License

Open Source License

Declaration

public static String downloadFromUrl(String urlString, PrintStream logger) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String downloadFromUrl(String urlString, PrintStream logger) throws IOException {

        logger.println("downloadFromUrl: " + urlString);
        InputStream is = null;// w  w w.ja v  a2  s.c  o  m
        FileOutputStream fos = null;
        long timeStamp = System.currentTimeMillis();

        URL url = new URL(urlString);

        File tempFile = File.createTempFile("instrumented-" + timeStamp, ".apk");

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

            is = urlConn.getInputStream(); //get connection inputstream
            fos = new FileOutputStream(tempFile); //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 {
            try {
                if (is != null) {
                    is.close();
                }
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }
        }

        return tempFile.getPath();
    }
}

Related

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