封装的FTP操作类

时间:2020-12-10 14:11:12

项目中用到的一个FTP操作类,实现了:下载文件Download()和获取FTP服务器上文件列表信息GetFileList()

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.IO;
using  System.Net;
using  System.Windows.Forms;

namespace  PCMDataImporter
{
    
///   <summary>
    
///  封装了FTP的两个操作:下载文件Download()和获取FTP服务器上文件列表信息GetFileList()
    
///   </summary>
     public   class  ftpOperater
    {
        
private   string  ftpServerIP;
        
private   string  ftpUser;
        
private   string  ftpPwd;

        
private  SystemParaReader spReader;

        
public  ftpOperater()
        {
            spReader 
=   new  SystemParaReader();
            
this .ftpServerIP  =  spReader.GetSysParaValue( " ftpServer " );
            
this .ftpUser  =  spReader.GetSysParaValue( " ftpUser " );
            
this .ftpPwd  =  spReader.GetSysParaValue( " ftpPwd " );
        }

        
///   <summary>
        
///  获取ftp服务器上的文件信息
        
///   </summary>
        
///   <returns> 存储了所有文件信息的字符串数组 </returns>
         public   string [] GetFileList()
        {
            
string [] downloadFiles;
            StringBuilder result 
=   new  StringBuilder();
            FtpWebRequest reqFTP;
            
try
            {
                reqFTP 
=  (FtpWebRequest)FtpWebRequest.Create( new  Uri( " ftp:// "   +  ftpServerIP  +   " / " ));
                reqFTP.UseBinary 
=   true ;
                reqFTP.Credentials 
=   new  NetworkCredential(ftpUser, ftpPwd);
                reqFTP.Method 
=  WebRequestMethods.Ftp.ListDirectory;
                WebResponse response 
=  reqFTP.GetResponse();
                StreamReader reader 
=   new  StreamReader(response.GetResponseStream());

                
string  line  =  reader.ReadLine();
                
while  (line  !=   null )
                {
                    result.Append(line);
                    result.Append(
" \n " );
                    line 
=  reader.ReadLine();
                }
                result.Remove(result.ToString().LastIndexOf(
' \n ' ),  1 );
                reader.Close();
                response.Close();

                
return  result.ToString().Split( ' \n ' );
            }
            
catch  (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(
" 获取文件信息失败: " + ex.Message, " 操作失败 " ,MessageBoxButtons.OK,MessageBoxIcon.Error);
                downloadFiles 
=   null ;
                
return  downloadFiles;
            }
        }

        
///   <summary>
        
///  获取FTP上指定文件的大小
        
///   </summary>
        
///   <param name="filename"> 文件名 </param>
        
///   <returns> 文件大小 </returns>
         public   long  GetFileSize( string  filename)
        {
            FtpWebRequest reqFTP;
            
long  fileSize  =   0 ;
            
try
            {
                reqFTP 
=  (FtpWebRequest)FtpWebRequest.Create( new  Uri( " ftp:// "   +  ftpServerIP  +   " / "   +  filename));
                reqFTP.Method 
=  WebRequestMethods.Ftp.GetFileSize;
                reqFTP.UseBinary 
=   true ;
                reqFTP.Credentials 
=   new  NetworkCredential(ftpUser, ftpPwd);
                FtpWebResponse response 
=  (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream 
=  response.GetResponseStream();
                fileSize 
=  response.ContentLength;

                ftpStream.Close();
                response.Close();
            }
            
catch  (Exception ex)
            {
                MessageBox.Show(
" 获取文件大小时,出现异常:\n "   +  ex.Message,  " 获取文件大小失败! " , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
return  fileSize;
        }

        
///   <summary>
        
///  实现ftp下载操作
        
///   </summary>
        
///   <param name="filePath"> 保存到本地的文件名 </param>
        
///   <param name="fileName"> 远程文件名 </param>
         public   void  Download( string  filePath,  string  fileName)
        {
            FtpWebRequest reqFTP;
            
try
            {
                
// filePath = <<The full path where the file is to be created.>>, 
                
// fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
                FileStream outputStream  =   new  FileStream(filePath  +   " \\ "   +  fileName, FileMode.Create);

                reqFTP 
=  (FtpWebRequest)FtpWebRequest.Create( new  Uri( " ftp:// "   +  ftpServerIP  +   " / "   +  fileName));
                reqFTP.Method 
=  WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary 
=   true ;
                reqFTP.Credentials 
=   new  NetworkCredential(ftpUser, ftpPwd);
                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();
            }
            
catch  (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}