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ī)名稱(chēng)</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">共享目錄路徑+文件名稱(chēng)</param>
/// <param name="local">本地路徑</param>
/// <param name="username">用戶(hù)名</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ù)傳多線程下載客戶(hù)端工具類(lèi)
- C#服務(wù)端圖片打包下載實(shí)現(xiàn)代碼解析
- c# 實(shí)現(xiàn)文件上傳下載功能的實(shí)例代碼
- C#怎樣實(shí)現(xiàn)文件下載斷點(diǎn)續(xù)傳
相關(guān)文章
Unity3D Shader實(shí)現(xiàn)鏡子效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)鏡子效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
C# 全角和半角轉(zhuǎn)換以及判斷的簡(jiǎn)單代碼
這篇文章介紹了在C#中判斷和轉(zhuǎn)換全角半角的方法,有需要的朋友可以參考一下2013-07-07
10分鐘學(xué)會(huì)VS NuGet包私有化部署
本文主要介紹了10分鐘學(xué)會(huì)VS NuGet包私有化部署,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
2021-09-09 
