Here you can find the source of writeFileFromUrl(URL sourceUrl, File targetFile)
public static void writeFileFromUrl(URL sourceUrl, File targetFile) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class Main { public static void writeFileFromUrl(URL sourceUrl, File targetFile) throws IOException { HttpURLConnection connection = (HttpURLConnection) sourceUrl.openConnection(); connection.setRequestMethod("GET"); InputStream in = connection.getInputStream(); FileOutputStream out = new FileOutputStream(targetFile); FileCopy(in, out, 1024);//from w w w. j a v a 2 s . c om out.close(); } public static void FileCopy(InputStream input, OutputStream output, int bufferSize) throws IOException { byte[] buf = new byte[bufferSize]; int n = input.read(buf); while (n >= 0) { output.write(buf, 0, n); n = input.read(buf); } output.flush(); } }