Java: Download File

The following code will allow you to download a file in java from a source URL to a target File.

public static void download(URL orig, File dest) throws IOException {
	OutputStream os = null;
	InputStream is = null;
	try {
		byte[] buf;
		int count, length = 0;
		os = new BufferedOutputStream(new FileOutputStream(dest));
		is = orig.openConnection().getInputStream();
		buf = new byte[4096];
		while ((count = != -1) {
			os.write(buf, 0, count);
			length += count;
	} finally {

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.