C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
更新時(shí)間:2015年08月22日 12:38:22 作者:我心依舊
這篇文章主要介紹了C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法,涉及C#文件傳輸?shù)募记?具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
/// <summary> /// 下載局域網(wǎng)文件 /// </summary> /// <param name="path">文件路徑,如:\\192.168.10.1\app\app\123.zip</param> /// <param name="username">計(jì)算機(jī)名稱</param> /// <param name="password">計(jì)算機(jī)密碼</param> static void RequestWindowsShared(string path, string username, string password) { //文件總大小 int allBytesCount = 0; //每次傳輸大小 int byteTemp = 1024; //當(dāng)前位置 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; //當(dāng)前位置 int bytePosition = 0; //剩下大小 int remain = 0; System.Net.FileWebRequest request = null; System.IO.Stream stream = null; try { //時(shí)間戳 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"; //設(shè)置獲得響應(yīng)的超時(shí)時(shí)間(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(); } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn)
- C# FileStream實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳
- C# 文件下載之?dāng)帱c(diǎn)續(xù)傳實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載的方法
- c#實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能示例分享
- C#實(shí)現(xiàn)支持?jǐn)帱c(diǎn)續(xù)傳多線程下載客戶端工具類
- C#服務(wù)端圖片打包下載實(shí)現(xiàn)代碼解析
- c# 實(shí)現(xiàn)文件上傳下載功能的實(shí)例代碼
- C#怎樣實(shí)現(xiàn)文件下載斷點(diǎn)續(xù)傳
相關(guān)文章
C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實(shí)例
下面小編就為大家分享一篇C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11C#利用Random得隨機(jī)數(shù)求均值、方差、正態(tài)分布的方法
這篇文章主要介紹了C#利用Random得隨機(jī)數(shù)求均值、方差、正態(tài)分布的方法,涉及C#數(shù)學(xué)運(yùn)算及概率統(tǒng)計(jì)的相關(guān)技巧,需要的朋友可以參考下2015-05-05C#實(shí)現(xiàn)統(tǒng)計(jì)字?jǐn)?shù)功能的方法
這篇文章主要介紹了C#實(shí)現(xiàn)統(tǒng)計(jì)字?jǐn)?shù)功能的方法,較為詳細(xì)的分析了C#字?jǐn)?shù)統(tǒng)計(jì)功能的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#實(shí)現(xiàn)一個(gè)控制臺(tái)的點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)一個(gè)控制臺(tái)的點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11