Request to download file in java net - opinion you
Save (download) a file via HTTP
Implementations[edit]
PHP[edit]
$array_of_lines = file($http);Java[edit]
Make sure your class imports these packages:
import www.cronistalascolonias.com.ar*; import www.cronistalascolonias.com.ar*;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)www.cronistalascolonias.com.arnnection(); //set cache and request method settings www.cronistalascolonias.com.arCaches(false); www.cronistalascolonias.com.arutput(false); //set other headers www.cronistalascolonias.com.aruestProperty ("Content-Type", "application/x-www-form-urlencoded"); www.cronistalascolonias.com.aruestProperty ("Cookie", cookie); //connect www.cronistalascolonias.com.art(); BufferedInputStream in = new BufferedInputStream(www.cronistalascolonias.com.arutStream()); OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(savePathAndFilename))); byte[] buf = new byte[]; int n = 0; while ((n=www.cronistalascolonias.com.ar(buf))>=0) { www.cronistalascolonias.com.ar(buf, 0, n); } www.cronistalascolonias.com.ar(); www.cronistalascolonias.com.ar(); 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)www.cronistalascolonias.com.arnnection(); //connect www.cronistalascolonias.com.art(); //input stream BufferedInputStream in = new BufferedInputStream(www.cronistalascolonias.com.arutStream()); //save the file OutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile)); byte[] buf = new byte[]; int n = 0; while ((n=www.cronistalascolonias.com.ar(buf))>=0) { www.cronistalascolonias.com.ar(buf, 0, n); } www.cronistalascolonias.com.ar(); www.cronistalascolonias.com.ar(); return true; }OCaml[edit]
open Http_www.cronistalascolonias.com.arience let get_and_print url = let s = http_get url in print_string s; flush stdoutPerl[edit]
use LWP::Simple qw(getstore); getstore($url, $file);Also see the source of , it is by default installed when you install the package libwww-perl. 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.
-
-