欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

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

来源:本站原创|时间:2020-01-10|栏目: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#程序设计有所帮助。

上一篇:winform实现创建最前端窗体的方法

栏    目:C#教程

下一篇:C#自定义签名章实现方法

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

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/7041.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

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

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有