Save (download) a file via HTTP

From CodeCodex

Implementations[edit]

PHP[edit]

  $array_of_lines = file($http);

Java[edit]

Make sure your class imports these packages:

import java.io.*;
import java.net.*;

Then use this method:

public static boolean downloadFileFromURL(String fetchUrl, String cookie, String savePathAndFilename)
throws IOException,FileNotFoundException,IOException {
    
    HttpURLConnection c;
    
    //save file    	
    URL url = new URL(fetchUrl);
    c = (HttpURLConnection)url.openConnection();
	
    //set cache and request method settings
    c.setUseCaches(false);
    c.setDoOutput(false);
	
    //set other headers
    c.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
    c.setRequestProperty ("Cookie", cookie);
	
    //connect
    c.connect();
	
    BufferedInputStream in = new BufferedInputStream(c.getInputStream());

    OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(savePathAndFilename)));
    byte[] buf = new byte[256];
    int n = 0;
    while ((n=in.read(buf))>=0) {
       out.write(buf, 0, n);
    }
    out.flush();
    out.close();
   
    return true;	    
}

Here is a simpler method if you don't need to set specific settings:

	public static boolean downloadFileFromURL(String fetchUrl, File saveFile)
	throws IOException,FileNotFoundException,IOException {
	    
	    HttpURLConnection c;
	    
	    //save file    	
	    URL url = new URL(fetchUrl);
	    c = (HttpURLConnection)url.openConnection();
		
	    //connect
	    c.connect();
		
	    //input stream
	    BufferedInputStream in = new BufferedInputStream(c.getInputStream());

	    //save the file
	    OutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
	    byte[] buf = new byte[256];
	    int n = 0;
	    while ((n=in.read(buf))>=0) {
	       out.write(buf, 0, n);
	    }
	    out.flush();
	    out.close();
	   
	    return true;	    
	}

OCaml[edit]

open Http_client.Convenience

let get_and_print url =
  let s = http_get url in
  print_string s;
  flush stdout

Perl[edit]

use LWP::Simple qw(getstore);
getstore($url, $file);

Also see the source of /usr/bin/lwp-download, it is by default installed when you install the package libwww-perl. lwp-download deals with a whole lot of craziness occuring on the wretched Web, like Content-Disposition headers, potentially harmful strings sent by the server, choosing automatically an appropriate filename, interrupted transfers etc.