[C#] 纯文本查看 复制代码
#region 以断点续传方式下载文件
/// <summary>
/// 以断点续传方式下载文件
/// </summary>
/// <param name="strFileName">下载文件的保存路径</param>
/// <param name="strUrl">文件下载地址</param>
public void DownloadFile(string strFileName, string strUrl)
{
//打开上次下载的文件或新建文件
long SPosition = 0;
FileStream FStream;
if (File.Exists(strFileName))
{
FStream = File.OpenWrite(strFileName);
SPosition = FStream.Length;
FStream.Seek(SPosition, SeekOrigin.Current);//移动文件流中的当前指针
}
else
{
FStream = new FileStream(strFileName, FileMode.Create);
SPosition = 0;
}
try
{
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
if (SPosition > 0)
myRequest.AddRange((int)SPosition);//设置Range值
//向服务器请求,获得服务器的回应数据流
Stream myStream = myRequest.GetResponse().GetResponseStream();
byte[] btContent = new byte[512];
int intSize = 0;
intSize = myStream.Read(btContent, 0, 512);
while (intSize > 0)
{
FStream.Write(btContent, 0, intSize);
intSize = myStream.Read(btContent, 0, 512);
}
FStream.Close();
myStream.Close();
//文件下载完成
}
catch
{
FStream.Close();
}
}
#endregion