-
-
Save BaoXiaolu/75c59dbf32b605cdc3ac to your computer and use it in GitHub Desktop.
C# Http文件下载公共类(支持断点续传)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Net; | |
namespace XcDownLoadFile | |
{ | |
public class DownLoadFile | |
{ | |
/// | |
/// 下载文件方法 | |
/// | |
/// 文件保存路径和文件名 | |
/// 返回服务器文件名 | |
/// | |
public bool DeownloadFile(string strFileName, string file) | |
{ | |
bool flag = false; | |
// 打开上次下载的文件 | |
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("http://www.newxing.com/download/" + file); | |
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(); | |
flag = true; // 返回true下载成功 | |
} | |
catch (Exception) | |
{ | |
FStream.Close(); | |
flag = false; // 返回false下载失败 | |
} | |
return flag; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment