从FTP下载文件带进度条

原文链接: http://www.cnblogs.com/-040506/p/3501946.html
  1     #region 获取FTP服务器上指定文件的大小
  2         /// 
  3         /// 获取FTP服务器上指定文件的大小
  4         /// 
  5         /// FTP服务器上的文件名
  6         /// 
  7         private int GetFtpFileSize(string fileName)
  8         {
  9             FtpWebRequest reqFTP;
 10             int fileSize = 0;
 11             try
 12             {
 13                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://211.149.150.137/feibiht/upLoad/rar/" + fileName));
 14 
 15                 reqFTP.Credentials = new NetworkCredential("Administrator", "02887140168");
 16 
 17                 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
 18 
 19                 reqFTP.UseBinary = true;
 20                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
 21                 Stream ftpStream = response.GetResponseStream();
 22                 fileSize = (int)response.ContentLength;
 23 
 24                 ftpStream.Close();
 25                 response.Close();
 26             }
 27             catch (Exception ex)
 28             {
 29                 MessageBox.Show(ex.Message);
 30             }
 31             return fileSize;
 32         }
 33         #endregion
 34 
 35 
 36         #region FTP下载带进度条
 37         /// 
 38         /// FTP下载带进度条
 39         /// 
 40         /// 下载到客户端的地址
 41         /// FTP上对应的文件名
 42         /// 
 43         private bool FtpDownload(string filePath, string fileName)
 44         {
 45             
 46             FtpWebRequest reqFTP;
 47             try
 48             {
 49                 //打开一个文件流 (System.IO.FileStream) 去读要下载的文件
 50                 FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
 51 
 52                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://211.149.150.137/feibiht/upLoad/rar/" + fileName));
 53                 // ftp用户名和密码
 54                 reqFTP.Credentials = new NetworkCredential("Administrator", "02887140168");
 55                 // 指定执行什么命令
 56                 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
 57                 // 指定数据传输类型
 58                 reqFTP.UseBinary = true;
 59 
 60                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
 61 
 62                 Stream ftpStream = response.GetResponseStream();
 63                 //这里其实娶不到FTP上该文件的大小,应另写方法来获取(WebRequestMethods.Ftp.GetFileSize)
 64                 //long cl = response.ContentLength;
 65                 // 缓冲大小设置为2kb
 66                 int bufferSize = 8192;// 4096;// 2048; //局域网内可以放大点
 67 
 68                 int readCount;
 69 
 70                 byte[] buffer = new byte[bufferSize];
 71 
 72                 readCount = ftpStream.Read(buffer, 0, bufferSize);
 73                 //FTP上文件的大小
 74                 int allbye = this.GetFtpFileSize(fileName);
 75                 int startbye = 0;
 76                 this.prcBar.Maximum = allbye;
 77                 this.prcBar.Minimum = 0;
 78                 this.prcBar.Visible = true;
 79                 this.label1.Visible = true;
 80                 // 流内容没有结束
 81                 while (readCount > 0)
 82                 {
 83                     // 把内容从file stream 写入 DownLoadStream
 84                     outputStream.Write(buffer, 0, readCount);
 85 
 86                     readCount = ftpStream.Read(buffer, 0, bufferSize);
 87                     //
 88                     startbye += readCount;
 89                     this.label1.Text = "已下载:" + (int)(startbye / 1024) + "KB/" + "总长度:" + (int)(allbye / 1024) + "KB" + " " + " 文件名:" + fileName;
 90                     prcBar.Value = startbye;
 91                     Application.DoEvents();
 92                     Thread.Sleep(5);
 93                 }
 94                 //this.prcBar.Visible = false;
 95                 this.Visible = false;
 96                 MessageBox.Show("文件下载成功!!!","提示");
 97                 //this.label1.Text = "下载成功!";
 98                 ftpStream.Close();
 99                 outputStream.Close();
100                 response.Close();
101                 return true;
102 
103             }
104             catch (Exception ex)
105             {
106                 MessageBox.Show(ex.Message);
107                 return false;
108             }
109             finally
110             {
111             }
112         }
113         #endregion
View Code

 

转载于:https://www.cnblogs.com/-040506/p/3501946.html

你可能感兴趣的:(从FTP下载文件带进度条)