Here you can find the source of download(String url, String toFile)
public static void download(String url, String toFile) throws MalformedURLException, IOException
//package com.java2s; /*// w w w. ja va 2s . c o m * Copyright 2011 ancoron. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; public class Main { private final static int BUFFER = 4 * 1024; private static final Logger log = Logger.getLogger("glassfishtest"); public static void download(String url, String toFile) throws MalformedURLException, IOException { log.log(Level.INFO, "Downloading {0} ...", url); long startTime = System.currentTimeMillis(); URL intUrl = new URL(url); intUrl.openConnection(); InputStream reader = intUrl.openStream(); /* * Setup a buffered file writer to write * out what we read from the website. */ FileOutputStream writer = new FileOutputStream(toFile); try { byte[] buffer = new byte[BUFFER]; int totalBytesRead = 0; int bytesRead = 0; // log.info("Reading ZIP file 150KB blocks at a time.\n"); while ((bytesRead = reader.read(buffer)) > 0) { writer.write(buffer, 0, bytesRead); totalBytesRead += bytesRead; } long endTime = System.currentTimeMillis(); log.log(Level.INFO, "Done. {0} bytes read ({1} milliseconds).", new Object[] { new Integer(totalBytesRead).toString(), new Long(endTime - startTime).toString() }); } finally { writer.close(); reader.close(); } } }