/// <summary> /// 下载文件方法 /// </summary> /// <param name="localFilePath"></param> /// <param name="url"></param> /// <returns></returns> public static bool Download(string localFilePath, string url) { bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 string ParentDirectory = Path.GetDirectoryName(localFilePath); if (!Directory.Exists(ParentDirectory)) { Directory.CreateDirectory(ParentDirectory); } if (File.Exists(localFilePath)) { //打开要下载的文件 FStream = File.OpenWrite(localFilePath); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(localFilePath, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url); long length = myRequest.GetResponse().ContentLength; 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 e) { FStream.Close(); flag = false; //返回false下载失败 } return(flag); }
/// <summary> /// 下载文件 /// </summary> /// <param name="strFileName">保存文件的全路径</param> /// <param name="md5">md5</param> /// <param name="http">网络地址</param> /// <param name="callback">回调</param> /// <returns></returns> public static bool DownloadFile(string strFileName, string md5, string http, Action <int> callback, Action completed = null) { var flag = false; FileStream FStream = null; try { long SPosition = 0; if (File.Exists(strFileName)) { if (md5 == GetFileMd5(strFileName))//如果文件相同 { return(true); } FStream = File.OpenWrite(strFileName); SPosition = FStream.Length;//断点续传 FStream.Seek(SPosition, SeekOrigin.Current); } else { FStream = new FileStream(strFileName, FileMode.Create); SPosition = 0; } var myRequest = (HttpWebRequest)WebRequest.Create(http); if (SPosition > 0) { myRequest.AddRange((int)SPosition); } var myStream = myRequest.GetResponse().GetResponseStream(); var btContent = new byte[512]; if (myStream != null) { var intSize = myStream.Read(btContent, 0, 512); while (intSize > 0) { callback?.Invoke(intSize); FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 512); } FStream.Close(); myStream.Close(); } flag = true; } catch (Exception ex) { FStream?.Close(); flag = false; File.Delete(strFileName); } if (flag) { completed?.Invoke(); } return(flag); }
//移入ZIP类 /// <summary> /// 从指定站点下载文件,并保存在指定位置,开始位置,完成度(用于ProgressBar使用) /// </summary> /// <param name="strFileName">本地物理路径</param> /// <param name="strUrl">目标Url</param> public static void DownloadFile(string strFileName, string strUrl, int begin, string flag) { if (SafeSC.FileNameCheck(strFileName)) { throw new Exception(strFileName + "取消保存"); } //已完成的,1%长度 int CompletedLength = 0; long percent = 0; progStatus = "0"; string temp = "0"; long sPosstion = 0; //磁盘现盘文件的长度 //long count = 0;// count += sPosstion,从指定位置开始写入字节 FileStream FStream; if (File.Exists(strFileName)) //如果文件存在 { FStream = File.OpenWrite(strFileName); //打开继续写入,并从尾部开始,用于断点续传 sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针 } else { FStream = new FileStream(strFileName, FileMode.Create); sPosstion = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); //if (CompletedLength > 0) // myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件.. //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); long FileLength = webResponse.ContentLength;//文件大小 percent = FileLength / 100; Stream myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[1024]; //if (count <= 0) count += sPosstion;// //开始写入 int count = 0; while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完 { FStream.Write(btContent, 0, count); //知道有多少个数字节后再写入 CompletedLength += count; if (!(CompletedLength / percent).ToString().Equals(temp)) { temp = (CompletedLength / percent).ToString(); progStatus = temp; } } myStream.Close(); } finally { FStream.Close(); } }
/// /// 下载文件方法 /// /// 文件保存路径和文件名 /// 返回服务器文件名 /// bool DownloadFile(string DownUrl, string SaveFileName) { bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 if (File.Exists(SaveFileName)) { //打开要下载的文件 FStream = File.OpenWrite(SaveFileName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(SaveFileName, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(DownUrl); 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); }
/// <summary> /// 以断点续传方式下载文件 /// </summary> /// <param name="strFileName">下载文件的保存路径</param> /// <param name="strUrl">文件下载地址</param> public static void DownloadFile(string strUrl) { var strFileName = AppDomain.CurrentDomain.BaseDirectory + @"DownloadGeam\" + strUrl.Substring(strUrl.LastIndexOf("/") + 1); float percent = 0; //打开上次下载的文件或新建文件 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值 } long totalBytes = myRequest.ContentLength; //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); byte[] btContent = new byte[1024]; int intSize = 0; long totalDownloadedByte = 0; intSize = myStream.Read(btContent, 0, 1024); while (intSize > 0) { totalDownloadedByte = intSize + totalDownloadedByte; FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 1024); percent = totalDownloadedByte / (float)totalBytes * 100; } FStream.Close(); myStream.Close(); } catch (Exception ex) { FStream.Close(); } }
/// <summary> /// 以断点续传方式下载文件 /// </summary> /// <param name="strFileName">下载文件的保存路径</param> /// <param name="strUrl">文件下载地址</param> public void DownloadFile(string strFileName, string strUrl) { //打开上次下载的文件或新建文件 int CompletedLength = 0;//记录已完成的大小 FileStream FStream; if (File.Exists(strFileName)) { FStream = File.OpenWrite(strFileName); sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current);//移动文件流中的当前指针 } else { FStream = new FileStream(strFileName, FileMode.Create); sPosstion = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); if (CompletedLength > 0) { myRequest.AddRange((int)CompletedLength);//设置Range值 } //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); FileLength = webResponse.ContentLength;//文件大小 Stream myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[1024]; if (count <= 0) { count += sPosstion; } while ((CompletedLength = myStream.Read(btContent, 0, 1024)) > 0) { FStream.Write(btContent, 0, CompletedLength); count += CompletedLength; } FStream.Close(); myStream.Close(); MessageBox.Show("文件下载完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { FStream.Close(); } }
/// <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(); MessageBox.Show("文件下载完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { FStream.Close(); } }
public static bool Download(string sourceFile, string desFile) { bool flag = false; long SPosition = 0; FileStream FStream = null; Stream myStream = null; string fileName = sourceFile.Substring(sourceFile.LastIndexOf(@"/") + 1); if (desFile.EndsWith("\\")) { desFile = desFile + fileName; } else { desFile = desFile + "\\" + fileName; } try { long serverFileLength = 0; //判断要下载的文件夹是否存在 if (File.Exists(desFile)) { //打开上次下载的文件 FStream = File.OpenWrite(desFile); //获取已经下载的长度 SPosition = FStream.Length; serverFileLength = GetHttpLength(sourceFile); if (SPosition == serverFileLength) {//文件是完整的,直接结束下载任务 return(true); } FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(desFile, FileMode.Create); SPosition = 0; } //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(sourceFile); if (SPosition > 0) { myRequest.AddRange(SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 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); } flag = true; //返回true下载成功 } catch (Exception ex) { System.Console.WriteLine("下载文件时异常:" + ex.Message + ex.StackTrace); } finally { //关闭流 if (myStream != null) { myStream.Close(); myStream.Dispose(); } if (FStream != null) { FStream.Close(); FStream.Dispose(); } } return(flag); }
/// <summary> /// 从指定服务器上下载文件,支持断点续传 /// </summary> /// <param name="url">目标Url</param> /// <param name="vpath">本地虚拟路径</param> /// <param name="begin">开始位置,默认为0</param> public void DownFile(string url, string vpath, int begin = 0) { vpath = SafeSC.PathDeal(vpath); if (SafeSC.FileNameCheck(vpath)) { throw new Exception("不支持下载[" + Path.GetFileName(vpath) + "]文件"); } string ppath = function.VToP(vpath); //已完成的,1%长度 int CompletedLength = 0; long percent = 0; string temp = "0"; long sPosstion = 0;//磁盘现盘文件的长度 //long count = 0;// count += sPosstion,从指定位置开始写入字节 FileStream FStream; if (File.Exists(ppath)) { FStream = File.OpenWrite(ppath); //打开继续写入,并从尾部开始,用于断点续传(如果不需要,则应该删除其) sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针 } else { string dir = Path.GetDirectoryName(ppath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } FStream = new FileStream(ppath, FileMode.Create); sPosstion = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url); //if (CompletedLength > 0) // myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件.. //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); long FileLength = webResponse.ContentLength;//文件大小 percent = FileLength / 100; Stream myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[1024]; //if (count <= 0) count += sPosstion;// //开始写入 int count = 0; while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完 { FStream.Write(btContent, 0, count); //知道有多少个数字节后再写入 CompletedLength += count; if (!(CompletedLength / percent).ToString().Equals(temp)) { temp = (CompletedLength / percent).ToString(); //progStatus = temp; } } myStream.Close(); } finally { FStream.Close(); } }
/// /// 下载文件方法 支持断点续传 /// /// 文件保存路径和文件名 /// 服务器文件名 public bool DownloadFile(CookieContainer cookie, string strFileName, string fileUrl) { bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件是否存在 if (File.Exists(strFileName)) { //打开要下载的文件 FStream = File.OpenWrite(strFileName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); //File.Delete(strFileName); } else { //文件不保存创建一个文件 FStream = new FileStream(strFileName, FileMode.Create); SPosition = 0; } HttpWebRequest myRequest = null; HttpWebResponse myResponse = null; try { //打开网络连接 myRequest = (HttpWebRequest)HttpWebRequest.Create(GetUrl(fileUrl)); //myRequest.AllowAutoRedirect = true; myRequest.CookieContainer = cookie; //OnDownloadProgress(SPosition, total ,strFileName ); if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 myResponse = myRequest.GetResponse() as HttpWebResponse; //myResponse.Headers.Add("content-reanghe", SPosition.ToString() ); long total = myResponse.ContentLength; Stream myStream = myResponse.GetResponseStream(); //定义一个字节数据 int bufflength = 1024; byte[] btContent = new byte[bufflength]; int intSize = 0; intSize = myStream.Read(btContent, 0, bufflength); SPosition += intSize; OnDownloadProgress(SPosition, total, strFileName); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, bufflength); SPosition += intSize; OnDownloadProgress(SPosition, total, strFileName); } myStream.Close(); flag = true; //返回true下载成功 OnDownloadCompleted(null, true, null); } catch (Exception ex) { flag = false; //返回false下载失败 OnDownloadCompleted(ex, false, strFileName); } finally { //关闭流 if (FStream != null) { FStream.Close(); FStream = null; } if (myResponse != null) { myResponse.Close(); myResponse = null; } if (myRequest != null) { myRequest.Abort(); myRequest = null; } } return(flag); }
//Html必须编码后再传输,html默认的是将其UrlEncode后传递,服务端再编辑,直接传是不能的 //public M_Http_Result UploadParam(string url, Dictionary<string, object> postParameters) //{ // HttpMultipartFormRequest req = new HttpMultipartFormRequest(); // //req.config = config; // M_Http_Result result = new M_Http_Result(); // result = req.AsyncHttpRequest(url, postParameters, null); // return result; //} #endregion #region 文件下载 /// <summary> /// 从指定服务器上下载文件,支持断点续传 /// </summary> /// <param name="url">目标Url</param> /// <param name="vpath">本地虚拟路径</param> /// <param name="begin">开始位置,默认为0</param> public static void DownFile(string url, string vpath, int begin = 0) { //尝试除以0,原因:下载的文字过小,占用字节为0 vpath = SafeSC.PathDeal(vpath); if (SafeSC.FileNameCheck(vpath)) { throw new Exception("不支持下载[" + Path.GetFileName(vpath) + "]文件"); } string ppath = function.VToP(vpath); //long percent = 0; long sPosstion = 0;//磁盘现盘文件的长度 //long count = 0;// count += sPosstion,从指定位置开始写入字节 FileStream FStream; if (File.Exists(ppath)) { FStream = File.OpenWrite(ppath); //打开继续写入,并从尾部开始,用于断点续传(如果不需要,则应该删除其) sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针 } else { string dir = Path.GetDirectoryName(ppath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } try { FStream = new FileStream(ppath, FileMode.Create); } catch (Exception ex) { throw new Exception(ex.Message + "||" + ppath); } sPosstion = 0; } //打开网络连接 Stream myStream = null; try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url); //if (CompletedLength > 0) // myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件.. //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); //long FileLength = webResponse.ContentLength;//文件大小 //percent = FileLength / 100; myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[1024]; //开始写入 int count = 0; while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完 { FStream.Write(btContent, 0, count); //知道有多少个数字节后再写入 } } catch (Exception ex) { if (!ex.Message.Contains("基础连接已经关闭")) { throw new Exception("DownFile:" + ex.Message + "|" + url); } } finally { if (myStream != null) { myStream.Close(); } if (FStream != null) { FStream.Close(); } } }
void SocketManager_BinaryInput(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd)) { switch (cmd) { case 2001: { long Key; if (read.ReadInt64(out Key)) { if (Key == down.DownKey) { string msg; if (read.ReadString(out msg)) { this.BeginInvoke(new EventHandler((a, b) => { MessageBox.Show(msg); this.Close(); })); } } } } break; case 2002: { long Key; if (read.ReadInt64(out Key)) { if (Key == down.DownKey) { long startp; long endp; byte[] buff; if (read.ReadInt64(out startp) && read.ReadInt64(out endp) && read.ReadByteArray(out buff)) { System.Threading.ThreadPool.QueueUserWorkItem((a) => { CheckB cb = IsCheckTable.Find(p => p.StartPostion == startp); if (cb != null) { if (cb.EndPostion == endp && buff.Length >= cb.Size) { cb.Checkd = true; FStream.Position = cb.StartPostion; FStream.Write(buff, 0, cb.Size); SizeR += cb.Size; this.BeginInvoke(new EventHandler((a1, b1) => { ProcessValue++; if (ProcessValue <= this.progressBar1.Maximum) { this.progressBar1.Value = ProcessValue; } else { this.progressBar1.Value = this.progressBar1.Maximum; } this.label1.Text = Math.Round(((double)SizeR / 1024 / 1024), 2) + "MB /" + Math.Round((double)down.Size / 1024 / 1024, 2) + "MB"; })); } } else { this.BeginInvoke(new EventHandler((a1, b1) => { BufferFormatV2 bufff = new BufferFormatV2((int)PackType.DownClose); bufff.AddItem(down.DownKey); SocketManager.Send(bufff.Finish()); MessageBox.Show("数据验证出错??"); Close(); })); } }); } } } } break; case 2003: { long Key; if (read.ReadInt64(out Key)) { if (Key == down.DownKey) { this.BeginInvoke(new EventHandler((a, b) => { CheckDown(); })); } } } break; case 2004: { long Key; if (read.ReadInt64(out Key)) { if (Key == down.DownKey) { long startP; byte[] xdata; if (read.ReadInt64(out startP) && read.ReadByteArray(out xdata)) { this.BeginInvoke(new EventHandler((a, b) => { CheckB cb = IsCheckTable.Find(p => p.StartPostion == startP); if (xdata.Length >= cb.Size) { cb.Checkd = true; FStream.Position = cb.StartPostion; FStream.Write(xdata, 0, cb.Size); SizeR += cb.Size; this.BeginInvoke(new EventHandler((a1, b1) => { ProcessValue++; this.progressBar1.Value = ProcessValue; this.label1.Text = Math.Round(((double)SizeR / 1024 / 1024), 2) + "MB /" + Math.Round((double)down.Size / 1024 / 1024, 2) + "MB"; })); } CheckDown(); })); } } } } break; } } }
//通过HTTP下载文件 public static bool DownloadFile(string url, string localFileName) { bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 if (File.Exists(localFileName)) { //打开要下载的文件 FStream = File.OpenWrite(localFileName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(localFileName, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url); if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); myRequest.ProtocolVersion = HttpVersion.Version11; myRequest.UserAgent = DefaultUserAgent; } if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 WebResponse response = (HttpWebResponse)myRequest.GetResponse(); Stream myStream = response.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 e) { MessageBox.Show(e.Message); FStream.Close(); flag = false; //返回false下载失败 } return(flag); }
/// <summary> /// 从台账服务器拿db文件http的方式 /// </summary> /// <param name="infoString"></param> /// <returns></returns> public static bool TZDown(string strFileName, string TZdz, string ftplj) { //将文件名改一下 strFileName = strFileName + ".wait"; 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(TZdz); if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); //定义一个字节数据 byte[] btContent = new byte[1024]; int intSize = 0; intSize = myStream.Read(btContent, 0, 1024); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 1024); } FStream.Flush(); //关闭流 FStream.Close(); myStream.Close(); //将文件名改回来 string tempName = strFileName; strFileName = strFileName.Substring(0, strFileName.Length - 5); File.Move(tempName, strFileName); flag = true; //返回true下载成功 } catch (Exception ex) { PublicMethod.write_state(ftplj + "\\", -1.01); CYZFramework.Log.CYZLog.writeLog(ex.ToString()); FStream.Close(); flag = false; //返回false下载失败 } return(flag); }
public bool Start() { if (SavePath == "" || Url == "") { return(false); } if (!SavePath.EndsWith("\\")) { SavePath += "\\"; } bool flag = false; long SPosition; FileStream FStream = null; Stream myStream = null; String desFile = SavePath + SaveFile + Suffix; try { //判断要下载的文件夹是否存在 if (File.Exists(desFile)) { //打开上次下载的文件 FStream = File.OpenWrite(desFile); //获取已经下载的长度 SPosition = FStream.Length; long serverFileLength = GetHttpLength(Url); if (SPosition == serverFileLength) {//文件是完整的,直接结束下载任务 return(true); } FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(desFile, FileMode.Create); SPosition = 0; } //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(Url); if (SPosition > 0) { myRequest.AddRange(SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 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); } flag = true; //返回true下载成功 } catch (Exception ex) { //下载文件时异常 } finally { //关闭流 if (myStream != null) { myStream.Close(); myStream.Dispose(); } if (FStream != null) { FStream.Close(); FStream.Dispose(); } } if (flag) { File.Move(desFile, SavePath + SaveFile); } return(flag); }
public static bool DownLoad_OffLine(string fileId, string UpLoadFileName, string DownloadFileName, string saveDir, int upSize, DataRow drmission) { bool flag = false; string uri = SysParams.FileServer + SysParams.FileServer_tmpFile + "/" + fileId + "/" + UpLoadFileName; //截取文件名 string fileName = UpLoadFileName; //构造文件完全限定名,准备将网络流下载为本地文件 string fileFullName = saveDir + DownloadFileName + SysParams.tmpDownLoadName; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 if (File.Exists(fileFullName)) { //打开要下载的文件 FStream = File.OpenWrite(fileFullName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(fileFullName, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(uri); if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); //定义一个字节数据 byte[] btContent = new byte[upSize]; int intSize = 0; intSize = myStream.Read(btContent, 0, upSize); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, upSize); lock (drmission.Table)//更新已上传量 { drmission["UploadSize"] = FStream.Length; } } //关闭流 FStream.Close(); myStream.Close(); flag = true; //返回true下载成功 } catch (Exception) { FStream.Close(); flag = false; //返回false下载失败 } return(flag); }