• C#Ftp的下载实例


    public  class FTPClient
        {
          private  string ftpServerIP="";//下载的ip
          private string ftpUserID="";  //用户名
          private string ftpPassword=""; //密码
          //带参的构造函数
          public FTPClient(string ftpServerIP, string ftpUserID, string ftpPassword) 
          {
            this.ftpPassword=ftpPassword;
            this.ftpUserID = ftpUserID;
            this.ftpServerIP = ftpServerIP;
          }
          /// <summary>
          /// 下载的方法
          /// </summary>
          /// <param name="filePath">要下载的路径</param>
          /// <param name="fileName">要下载的文件名,如果为空,则代表文件下的全部文件</param>
          public  void Download(string filePath, string fileName)
          {
              FtpWebRequest reqFTP;
    
              try
              {
                  FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);    //文件流暑促文件
    
                  reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName)); //创建ftpWebRequest
    
                  reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;           //ftp的方法,上传还是下载
    
                  reqFTP.UseBinary = true;              //是否使用二进制
    
                  reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);   //输入用户名和密码
    
                  FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
    
                  Stream ftpStream = response.GetResponseStream();  //获取文件流
    
                  long cl = response.ContentLength; //文件流的长度
    
                  int bufferSize = 2048;
    
                  int readCount;
    
                  byte[] buffer = new byte[bufferSize]; //字节转换
    
                  readCount = ftpStream.Read(buffer, 0, bufferSize);   
       
                      while (readCount > 0)
                      {
                          outputStream.Write(buffer, 0, readCount);
    
                          readCount = ftpStream.Read(buffer, 0, bufferSize); //读取文件
                      }
                  
                  ftpStream.Close();
                  outputStream.Close();
                  response.Close();
                  System.GC.Collect();
              }
              catch (Exception ex)
              {
                //  MessageBox.Show(ex.Message);
              }
          }
        }

    上面是FTp的后台服务端,下面是应用客户端:

    ftpServerIP = "192.168.2.211/Test/ssl/Image";
                ftpUserID = "123456";
                ftpPassword = "123456";
                FTPClient ftpClient = new FTPClient(ftpServerIP, ftpUserID, ftpPassword);
                ftpClient.Download(@"C:\picture", "3201.png");

    客户端可以输入ip,用户名,密码,要下载的路径或者有下载的文件,就可以执行了。如果下载文件为空,则代表下载ip底下的所有文件。

  • 相关阅读:
    max key length is 1000 bytes
    205 Reset Content
    The Path Attribute
    track message forwards, avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain
    test hypertext links for validity, accessibility, and recent modification
    ES6 will change the way you write JS code.
    ECMAScript Web APIs node.js
    Symbols
    HTML/Elements/base
    frag General URL components
  • 原文地址:https://www.cnblogs.com/shunxiyuan/p/2803195.html
Copyright © 2020-2023  润新知