只下载FTP文件更新的部分

 1         /// <summary>
 2         /// 下载
 3         /// </summary>
 4         /// <param name="filePath">下载后文件存放位置</param>
 5         /// <param name="fileName">文件名称</param>
 6         public bool Download(string filePath, string fileName)
 7         {
 8             bool judge = false;
 9             FtpWebRequest reqFTP = null;
10             Stream ftpStream = null;
11             FtpWebResponse response = null;
12             FileStream outputStream = null;
13 
14             try
15             {
16                 var path = filePath + "\\" + fileName;
17                 //获取本地文件
18                 //Append决定outputStream.Write写入时,是从头开始覆盖,还是从末尾追加。 
19                 outputStream = new FileStream(path, FileMode.Append);
20                 //本地文件字节数
21                 long outputStreamLength = outputStream.Length;
22                 //创建FTP请求
23                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));        
24                 //reqFTP.UsePassive = false;//会导致 操作超时
25                 //reqFTP.KeepAlive = false; 
26                 reqFTP.UseBinary = true;
27                 //配置FTP下载的开始位置:outputStreamLength
28                 reqFTP.ContentOffset = outputStreamLength;
29                 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
30                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
31                 //FTP服务器响应(或返回)结果
32                 response = (FtpWebResponse)reqFTP.GetResponse();
33                 //获取FTP发送的响应数据流
34                 ftpStream = response.GetResponseStream();
35                 int bufferSize = 2048;
36                 int readCount;
37                 byte[] buffer = new byte[bufferSize];
38                /*
39                 Stream 表示“流”(这个bai概念在计算机中非常常见,题主可以自行搜索相关资料),本质上是一种字节序列。说穿了,计算机只认识0和1,那么这么丰富多彩的文本、音乐、视频,归根结底都是转换成字节存储在内存与硬盘中的。
40                 Stream 对象有一个属性 Length,表示这个流的长度;还有一个属性 Position,表示这个流当前的位置。
41                 Stream.Read(byte[] array, int offset, int count);
42                 array 表示缓冲区;offset 表示从流的当前位置(也就上面说的 Position)偏移多少个字节开始读;count 表示读取多少个字节。
43                 该方法返回的是实际上读取了多少个字节(永远小于等于 count),如果该值等于 0,说明已经到流的末尾了。读取之后,这个流的 Position 就会发生变化。
44                 为什么要缓冲区?因为一个流很可能非常大,一次性的加载是不现实的,所以需要分块来读取,存储每个分块的这个字节数组就叫做“缓冲区”。
45               */   
46                readCount = ftpStream.Read(buffer, 0, bufferSize);
47       
48                 while (readCount > 0)
49                 {
50                     //将读取的字节数组buffer写入outputStream文件流
51                     outputStream.Write(buffer, 0, readCount);
52                     //再次读取,再次位置提升。不断重复直到readCount为0;
53                     readCount = ftpStream.Read(buffer, 0, bufferSize);
54                 }
55                 judge = true;
56                 ftpStream.Close();
57                 outputStream.Close();
58                 response.Close();
59                 
60             }
61             catch (Exception ex)
62             {
63 
64                 LogManage.Info(fileName);
65                 LogManage.Error(ex);
66 
67                 try
68                 {
69                     if (ftpStream != null)
70                     {
71                         ftpStream.Close();
72                     }
73                     if (outputStream != null)
74                     {
75                         outputStream.Close();
76                     }
77                     if (response != null)
78                     {
79                         response.Close();
80                     }
81                 }
82                 catch (Exception ex2)
83                 {
84 
85                 }
86             }
87  
88 
89             return judge;
90         }
View Code

相关文章: