时间:2021-05-20
本文实例讲述了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#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
复制代码代码如下:/*.Net/C#:实现支持断点续传多线程下载的HttpWeb客户端工具类(C#DIYHttpWebClient)*Reflector了一下S
本文实例讲述了php实现的支持断点续传的文件下载类及其用法,是非常实用的技巧。分享给大家供大家参考。具体方法如下:通常来说,php支持断点续传,主要依靠HTTP
技术栈前端用了React,后端则是EggJs,都用了TypeScript编写。断点续传实现原理断点续传就是在上传一个文件的时候可以暂停掉上传中的文件,然后恢复上
Android断点续传原理以及实现0.前言在Android开发中,断点续传听起来挺容易,在下载一个文件时点击暂停任务暂停,点击开始会继续下载文件。但是真正实现起
需求背景动态创建的文件下载的时候希望浏览器显示下载进度动态创建的文件希望能够分段下载HTTP断点续传报文要实现HTTP断点续传必须要简单了解以下几个报文。Acc