Difference between revisions of "Save (download) a file via HTTP"

From CodeCodex

(Java)
(Perl)
Line 81: Line 81:
 
   print_string s;
 
   print_string s;
 
   flush stdout
 
   flush stdout
 +
 +
===Perl===
 +
<HIGHLIGHTSYNTAX language="perl">
 +
use LWP::Simple qw(getstore);
 +
getstore($url, $file);
 +
</HIGHLIGHTSYNTAX>
 +
Also see the source of <tt>/usr/bin/lwp-download</tt>, it is by default installed when you install the package libwww-perl. <tt>lwp-download</tt> 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.
  
 
[[Category:I/O]]
 
[[Category:I/O]]
Line 86: Line 93:
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Objective Caml]]
 
[[Category:Objective Caml]]
 +
[[Category:Perl]]

Revision as of 19:40, 17 June 2007

Implementations

Java

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

open Http_client.Convenience

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

Perl

<HIGHLIGHTSYNTAX language="perl"> use LWP::Simple qw(getstore); getstore($url, $file); </HIGHLIGHTSYNTAX> 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.