Java URL Download downloadURL(String url, String outFile)

Here you can find the source of downloadURL(String url, String outFile)

Description

download URL

License

Creative Commons License

Declaration

public static void downloadURL(String url, String outFile)
            throws FileNotFoundException, MalformedURLException, IOException 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

import java.io.*;

import java.net.*;

public class Main {
    public static void downloadURL(String url, String outFile)
            throws FileNotFoundException, MalformedURLException, IOException {
        write((new URL(url)).openStream(), outFile);
    }//from   w  w w  .ja  va 2  s.c om

    public static void write(InputStream in, OutputStream out, int maxSizeInBytes) throws IOException {

        int totalBytes = 0;
        try {

            totalBytes = 0;
            int bytesRead = 0;
            byte[] buf = new byte[2048];
            while ((bytesRead = in.read(buf)) != -1) {
                out.write(buf, 0, bytesRead);
                totalBytes += bytesRead;
                if (totalBytes > maxSizeInBytes) {
                    throw new IOException("Write failed: input stream exceeded the maximum " + maxSizeInBytes
                            + " bytes in size.");
                }
            }
        } finally {
            if (out != null) {
                out.close();
            }
        }

    }

    public static void write(InputStream in, String outFile) throws FileNotFoundException, IOException {
        write(in, new File(outFile), Integer.MAX_VALUE);
    }

    public static void write(InputStream in, File outFile, int maxSizeInBytes)
            throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream(outFile);
        write(in, fos, maxSizeInBytes);
        fos.close();
    }
}

Related

  1. downloadToFile(URL url, File downloadFile)
  2. downloadToFile(URL url, File file)
  3. downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)
  4. downloadToString(String url)
  5. downloadURL(String addr, File outFile)
  6. downloadURL(URL url, File file)
  7. downloadURL(URL url, String localPath)
  8. downloadUrlToFile(String surl, File file, String method)
  9. downloadUrlToFile(URL url, File file)