Here you can find the source of downloadUrlToFile(URL url, File result)
Parameter | Description |
---|---|
url | a parameter |
result | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void downloadUrlToFile(URL url, File result) throws IOException
//package com.java2s; /**//from w ww . j a v a2s .c o m * <p> * Utilities for manipulating Paths, Files, Directories, etc. * </p> * <p> * <span class="BSDLicense"> This software is distributed under the <a * href="http://hci.stanford.edu/research/copyright.txt">BSD License</a>.</span> * </p> * * @author <a href="http://graphics.stanford.edu/~ronyeh">Ron B Yeh</a> (ronyeh(AT)cs.stanford.edu) */ import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class Main { /** * @param url * @param result * @throws IOException */ public static void downloadUrlToFile(URL url, File result) throws IOException { IOException exception = null; InputStream is = null; DataInputStream dis = null; FileOutputStream fos = null; byte[] buf = new byte[1024]; try { is = url.openStream(); dis = new DataInputStream(new BufferedInputStream(is)); fos = new FileOutputStream(result); int bytesRead; bytesRead = dis.read(buf); while (bytesRead > 0) { fos.write(buf, 0, bytesRead); bytesRead = dis.read(buf); } } catch (IOException ioe) { exception = ioe; } finally { try { if (is != null) is.close(); } catch (IOException ioe) { } try { if (fos != null) fos.close(); } catch (IOException ioe) { } if (exception != null) throw exception; } } }