1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.IO;6 usingSystem.Net;7 8 namespaceXcdownloadfile9 {Ten Public classDownLoadFile One { A /// - ///Download File Method - /// the ///file save path and filename - ///returns the server file name - /// - Public BOOLDeownloadfile (stringstrFileName,stringfile) + { - BOOLFlag =false; + //Open the last downloaded file A LongSposition =0; at //instantiating a Stream object - FileStream fstream; - //determine if the folder you want to download exists - if(File.exists (strFileName)) - { - //Open the file you want to download inFStream =File.openwrite (strFileName); - //get the length that has been downloaded toSposition =fstream.length; + Fstream.seek (sposition, seekorigin.current); - } the Else * { $ //file is not saved create a filePanax NotoginsengFStream =NewFileStream (strFileName, filemode.create); -Sposition =0; the } + Try A { the //Open Network Connection +HttpWebRequest myrequest = (HttpWebRequest) httpwebrequest.create ("http://www.newxing.com/download/"+file); - if(Sposition >0) $Myrequest.addrange ((int) sposition);//Set Range value $ //request to the server, get the response data stream of the server -Stream MyStream =Myrequest.getresponse (). GetResponseStream (); - //define one byte of data the byte[] Btcontent =New byte[ +]; - intIntSize =0;WuyiIntSize = Mystream.read (Btcontent,0, +); the while(IntSize >0) - { WuFstream.write (Btcontent,0, intSize); -IntSize = Mystream.read (Btcontent,0, +); About } $ //Close the stream - fstream.close (); - mystream.close (); -Flag =true;//return true Download succeeded A } + Catch(Exception) the { - fstream.close (); $Flag =false;//return false Download failed the } the returnFlag; the } the } -}
View Code
Thank you: http://www.cnblogs.com/skyay/p/3880221.html
C # http File download public class (support for breakpoint continuation)