zoukankan      html  css  js  c++  java
  • 从FTP下载文件带进度条

      1     #region 获取FTP服务器上指定文件的大小
      2         /// <summary>
      3         /// 获取FTP服务器上指定文件的大小
      4         /// </summary>
      5         /// <param name="fileName">FTP服务器上的文件名</param>
      6         /// <returns></returns>
      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         /// <summary>
     38         /// FTP下载带进度条
     39         /// </summary>
     40         /// <param name="filePath">下载到客户端的地址</param>
     41         /// <param name="fileName">FTP上对应的文件名</param>
     42         /// <returns></returns>
     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
  • 相关阅读:
    LInux常用命令:总结
    SpringBoot声明式事务(转)
    连接linux客户端工具
    查看servlet 3.0文档方法
    通过spring.io找spring历史版本
    归并排序(比希尔还要快)
    快速排序(比希尔排序还要快)
    希尔排序(交换式和移位式)
    插入排序
    选择排序(时间复杂度O(n^2))
  • 原文地址:https://www.cnblogs.com/-040506/p/3501946.html
Copyright © 2011-2022 走看看