CopyRemote::usage="CopyRemote[url, localfilename] copies a file from an http location to localfilename."; (* Example usage: CopyRemote["http://www.mertig.com/mathdepot/buttons/ButtonTools.nb", ToFileName[{$UserAddOnsDirectory,"SystemFiles","FrontEnd","Palettes"}, "ButtonTools.nb"]] *) (* You need JLink 2.0 or higher. this code is based on the GetRemote example in the JLink documentation *) Options[CopyRemote]={ProxyHost :> None, ProxyPort :> None}; CopyRemote[url_String /; StringMatchQ[url, "http://*.*", IgnoreCase-> True], localfile_:Automatic, opts___?OptionQ] :=( Needs["JLink`"]; JLink`JavaBlock[ Module[{u, stream, numRead, outFile, buf, prxyHost, prxyPort}, {prxyHost, prxyPort} = {ProxyHost, ProxyPort} /. Flatten[{opts}] /. Options[CopyRemote]; JLink`StartJava[]; If[StringQ[prxyHost], (* Set properties to force use of proxy. *) JLink`SetInternetProxy[prxyHost, prxyPort] ]; u = JLink`JavaNew["java.net.URL", url]; (* This is where the error will show up if the URL is not valid. A Java exception will be thrown during openStream, which causes the method to return $Failed. *) stream = u@openStream[]; If[stream === $Failed, Return[$Failed]]; buf = JLink`JavaNew["[B", 5000]; (* 5000 is an arbitrary buffer size *) If[StringQ[localfile], outFile = OpenWrite[localfile, DOSTextFormat -> False], outFile = OpenTemporary[DOSTextFormat->False]; ]; While[(numRead = stream@read[buf]) > 0, WriteString[outFile, FromCharacterCode[If[# < 0, #+256, #]& /@ Take[JLink`Val[buf], numRead]]] ]; stream@close[]; Close[outFile] (* Close returns the filename *) ] ] );