C#文件断点续传实现方法
作者:我心依旧
这篇文章主要介绍了C#文件断点续传实现方法,涉及C#文件传输的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了C#文件断点续传实现方法。分享给大家供大家参考。具体实现方法如下:
/// <summary> /// 下载局域网文件 /// </summary> /// <param name="path">文件路径,如:\\192.168.10.1\app\app\123.zip</param> /// <param name="username">计算机名称</param> /// <param name="password">计算机密码</param> static void RequestWindowsShared(string path, string username, string password) { //文件总大小 int allBytesCount = 0; //每次传输大小 int byteTemp = 1024; //当前位置 int bytePosition = 0; //剩下大小 int remain = 0; System.Net.FileWebRequest request = null; System.Net.FileWebResponse response = null; System.IO.Stream stream = null; System.IO.FileStream fileStream = null; try { Uri uri = new Uri(path); request = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uri); System.Net.ICredentials ic = new System.Net.NetworkCredential(username, password); request.Credentials = ic; response = (System.Net.FileWebResponse)request.GetResponse(); stream = response.GetResponseStream(); byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); string filename = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + System.IO.Path.GetFileName(path); fileStream = new FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); allBytesCount = bytes.Length; remain = allBytesCount; while (remain > 0) { fileStream.Write(bytes, bytePosition, byteTemp); remain = remain - byteTemp; bytePosition = bytePosition + byteTemp; fileStream.Flush(); if (remain < byteTemp) byteTemp = remain; } Console.WriteLine("下载成功!"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { fileStream.Close(); fileStream.Dispose(); stream.Close(); stream.Dispose(); } } /// <summary> /// 上传文件 /// </summary> /// <param name="path">共享目录路径+文件名称</param> /// <param name="local">本地路径</param> /// <param name="username">用户名</param> /// <param name="password">密码</param> static void ResponseWindowsShared(string path, string local, string username, string password) { //文件总大小 int allBytesCount = 0; //每次传输大小 int byteTemp = 1024; //当前位置 int bytePosition = 0; //剩下大小 int remain = 0; System.Net.FileWebRequest request = null; System.IO.Stream stream = null; try { //时间戳 string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x"); Uri uri = new Uri(path); byte[] bytes = System.IO.File.ReadAllBytes(local); request = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uri); request.Method = "POST"; //设置获得响应的超时时间(300秒) request.Timeout = 300000; request.ContentType = "multipart/form-data; boundary=" + strBoundary; request.ContentLength = bytes.Length; System.Net.ICredentials ic = new System.Net.NetworkCredential(username, password); request.Credentials = ic; stream = request.GetRequestStream(); allBytesCount = bytes.Length; remain = allBytesCount; while (remain > 0) { stream.Write(bytes, bytePosition, byteTemp); remain = remain - byteTemp; bytePosition = bytePosition + byteTemp; stream.Flush(); if (remain < byteTemp) byteTemp = remain; } Console.WriteLine("上传成功!"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { stream.Close(); stream.Dispose(); } }
希望本文所述对大家的C#程序设计有所帮助。