C#http下载文件
标签:tar else cat files www origin web 后缀 string
特点:
(1)下载文件加上downloading后缀,下载完成再去掉后缀,
(2)含有通知下载进度事件
(3)断点续传
public class DownloadHelper
{
private int ByteSize = 1024;
///
/// 下载中的后缀,下载完成去掉
///
private const string Suffix = ".downloading";
public event Action ShowDownloadPercent;
///
/// Http方式下载文件
///
/// http地址
/// 本地文件
///
public int DownloadFile(string url, string localfile)
{
int ret = 0;
string localfileReal = localfile;
string localfileWithSuffix = localfileReal + Suffix;
try
{
long startPosition = 0;
FileStream writeStream = null;
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(localfileReal))
return 1;
//取得远程文件长度
long remoteFileLength = GetHttpLength(url);
if (remoteFileLength == 0)
return 2;
if (File.Exists(localfileReal))
return 0;
//判断要下载的文件是否存在
if (File.Exists(localfileWithSuffix))
{
writeStream = File.OpenWrite(localfileWithSuffix);
startPosition = writeStream.Length;
if (startPosition > remoteFileLength)
{
writeStream.Close();
File.Delete(localfileWithSuffix);
writeStream = new FileStream(localfileWithSuffix, FileMode.Create);
}
else if (startPosition == remoteFileLength)
{
DownloadFileOk(localfileReal, localfileWithSuffix);
writeStream.Close();
return 0;
}
else
writeStream.Seek(startPosition, SeekOrigin.Begin);
}
else
writeStream = new FileStream(localfileWithSuffix, FileMode.Create);
HttpWebRequest req = null;
HttpWebResponse rsp = null;
try
{
req = (HttpWebRequest)HttpWebRequest.Create(url);
if (startPosition > 0)
req.AddRange((int)startPosition);
rsp = (HttpWebResponse)req.GetResponse();
using (Stream readStream = rsp.GetResponseStream())
{
byte[] btArray = new byte[ByteSize];
long currPostion = startPosition;
int contentSize = 0;
while ((contentSize = readStream.Read(btArray, 0, btArray.Length)) > 0)
{
writeStream.Write(btArray, 0, contentSize);
currPostion += contentSize;
if (ShowDownloadPercent != null)
ShowDownloadPercent((int)(currPostion * 100 / remoteFileLength));
}
}
}
catch (Exception ex)
{
Log.Info("获取远程文件失败!exception:\n" + ex.ToString());
ret = 3;
}
finally
{
if (writeStream != null)
writeStream.Close();
if (rsp != null)
rsp.Close();
if (req != null)
req.Abort();
if (ret == 0)
DownloadFileOk(localfileReal, localfileWithSuffix);
}
}
catch (Exception ex)
{
Log.Info("获取远程文件失败!exception:\n" + ex.ToString());
ret = 4;
}
return ret;
}
///
/// 下载完成
///
private void DownloadFileOk(string localfileReal, string localfileWithSuffix)
{
try
{
//去掉.downloading后缀
FileInfo fi = new FileInfo(localfileWithSuffix);
fi.MoveTo(localfileReal);
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
finally
{
//通知完成
if (ShowDownloadPercent != null)
ShowDownloadPercent(100);
}
}
// 从文件头得到远程文件的长度
private long GetHttpLength(string url)
{
long length = 0;
HttpWebRequest req = null;
HttpWebResponse rsp = null;
try
{
req = (HttpWebRequest)HttpWebRequest.Create(url);
rsp = (HttpWebResponse)req.GetResponse();
if (rsp.StatusCode == HttpStatusCode.OK)
length = rsp.ContentLength;
}
catch (Exception ex)
{
Log.Info("获取远程文件大小失败!exception:\n" + ex.ToString());
}
finally
{
if (rsp != null)
rsp.Close();
if (req != null)
req.Abort();
}
return length;
}
}
参考:http://www.cnblogs.com/hayden/archive/2012/04/26/2472815.html
C#http下载文件
标签:tar else cat files www origin web 后缀 string
原文地址:https://www.cnblogs.com/yaosj/p/10880577.html
文章来自:
搜素材网的
编程语言模块,转载请注明文章出处。
文章标题:
C#http下载文件
文章链接:http://soscw.com/index.php/essay/90199.html
评论