欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#文件断点续传实现方法

时间:2021-02-23 15:17:03|栏目:.NET代码|点击:

本文实例讲述了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#程序设计有所帮助。

上一篇:从ASP.NET得到Microsoft Word文档的代码

栏    目:.NET代码

下一篇:C#判断程序是否是管理员权限运行的方法代码示例

本文标题:C#文件断点续传实现方法

本文地址:http://www.codeinn.net/misctech/68537.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有