I'm Singaram Subramanian, and I work with CSC India as a Software Developer. My blog is an attempt to share my learnings with all (mainly, for those who desperately mine google finding ways to solve problems, fix issues, learn about a Java/Open source software, or deciding on tough choices etc. during software development as I do). Singaram is a DZone MVB and is not an employee of DZone and has posted 36 posts at DZone. You can read more from them at their website. View Full User Profile

Java: How to save / download a file available at a particular URL location in Internet?

  • submit to reddit
package singz.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

 * @author Singaram Subramanian
public class FileDownloadTest {

public static void main(String[] args) {

 // Make sure that this directory exists
 String dirName = "C:\\FileDownload";

 try {

System.out.println("Downloading \'Maven, Eclipse and OSGi working together\' PDF document...");

 dirName + "\\maven_eclipse_and_osgi_working_together.pdf",

 System.out.println("Downloaded \'Maven, Eclipse and OSGi working together\' PDF document.");

 System.out.println("Downloading \'InnoQ Web Services Standards Poster\' PDF document...");

 dirName + "\\innoq_ws-standards_poster_2007-02.pdf",

 System.out.println("Downloaded \'InnoQ Web Services Standards Poster\' PDF document.");

 } catch (MalformedURLException e) {
 } catch (IOException e) {


// Using Java IO
 public static void saveFileFromUrlWithJavaIO(String fileName, String fileUrl)
 throws MalformedURLException, IOException {
 BufferedInputStream in = null;
 FileOutputStream fout = null;
 try {
 in = new BufferedInputStream(new URL(fileUrl).openStream());
 fout = new FileOutputStream(fileName);

byte data[] = new byte[1024];
 int count;
 while ((count = in.read(data, 0, 1024)) != -1) {
 fout.write(data, 0, count);
 } finally {
 if (in != null)
 if (fout != null)

// Using Commons IO library
 // Available at http://commons.apache.org/io/download_io.cgi
 public static void saveFileFromUrlWithCommonsIO(String fileName,
 String fileUrl) throws MalformedURLException, IOException {
 FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));



From http://singztechmusings.wordpress.com/2011/12/20/java-how-to-save-download-a-file-available-at-a-particular-url-location-in-internet/

Published at DZone with permission of Singaram Subramanian, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



John Hendrikx replied on Tue, 2012/01/03 - 1:34am

Nice, but a bit behind the times. Here's it in Java 7:

    try (InputStream in = URI.create("http://java.sun.com/").toURL().openStream()) {
        Files.copy(in, Paths.get("C:/test.txt"));

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.