本文实例讲述了C#文件断点续传实现方法。分享给大家供大家参考。具体实现方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/// <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#程序设计有所帮助。