现在的位置: 首页 > 综合 > 正文

FTP上传类FTP上传类

2013年08月15日 ⁄ 综合 ⁄ 共 16447字 ⁄ 字号 评论关闭
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. using System.Net; 
  5. using System.IO; 
  6. using System.Globalization; 
  7. using System.Text.RegularExpressions; 
  8. namespace WebBaseLib 
  9.     /// <summary> 
  10.     /// FTP处理操作类 
  11.     /// 功能: 
  12.     /// 下载文件 
  13.     /// 上传文件 
  14.     /// 上传文件的进度信息 
  15.     /// 下载文件的进度信息 
  16.     /// 删除文件 
  17.     /// 列出文件 
  18.     /// 列出目录 
  19.     /// 进入子目录 
  20.     /// 退出当前目录返回上一层目录 
  21.     /// 判断远程文件是否存在 
  22.     /// 判断远程文件是否存在 
  23.     /// 删除远程文件    
  24.     /// 建立目录 
  25.     /// 删除目录 
  26.     /// 文件(目录)改名 
  27.   
  28.     /// </summary> 
  29.     /// <remarks> 
  30.     /// 创建人:南疯 
  31.     /// 创建时间:2007年4月28日 
  32.     /// </remarks> 
  33.     #region 文件信息结构 
  34.     public struct FileStruct 
  35.     { 
  36.         public string Flags; 
  37.         public string Owner; 
  38.         public string Group; 
  39.         public bool IsDirectory; 
  40.         public DateTime CreateTime; 
  41.         public string Name; 
  42.     } 
  43.     public enum FileListStyle 
  44.     { 
  45.         UnixStyle, 
  46.         WindowsStyle, 
  47.         Unknown 
  48.     } 
  49.     #endregion 
  50.     public class NewFtp 
  51.     { 
  52.         #region 属性信息 
  53.         /// <summary> 
  54.         /// FTP请求对象 
  55.         /// </summary> 
  56.         FtpWebRequest Request = null
  57.         /// <summary> 
  58.         /// FTP响应对象 
  59.         /// </summary> 
  60.         FtpWebResponse Response = null
  61.         /// <summary> 
  62.         /// FTP服务器地址 
  63.         /// </summary> 
  64.         private Uri _Uri; 
  65.         /// <summary> 
  66.         /// FTP服务器地址 
  67.         /// </summary> 
  68.         public Uri Uri 
  69.         { 
  70.             get 
  71.             { 
  72.                 if( _DirectoryPath ==
    "/"
  73.                 { 
  74.                     return _Uri; 
  75.                 } 
  76.                 else 
  77.                 { 
  78.                     string strUri = _Uri.ToString(); 
  79.                     if( strUri.EndsWith(
    "/" ) ) 
  80.                     { 
  81.                         strUri = strUri.Substring( 0, strUri.Length - 1 ); 
  82.                     } 
  83.                     return new Uri( strUri +
    this.DirectoryPath ); 
  84.                 } 
  85.             } 
  86.             set 
  87.             { 
  88.                 if( value.Scheme != Uri.UriSchemeFtp ) 
  89.                 { 
  90.                     throw
    new Exception( "Ftp 地址格式错误!" ); 
  91.                 } 
  92.                 _Uri = new Uri( value.GetLeftPart( UriPartial.Authority ) ); 
  93.                 _DirectoryPath = value.AbsolutePath; 
  94.                 if( !_DirectoryPath.EndsWith(
    "/" ) ) 
  95.                 { 
  96.                     _DirectoryPath +=
    "/"
  97.                 } 
  98.             } 
  99.         } 
  100.   
  101.         /// <summary> 
  102.         /// 当前工作目录 
  103.         /// </summary> 
  104.         private string _DirectoryPath; 
  105.   
  106.         /// <summary> 
  107.         /// 当前工作目录 
  108.         /// </summary> 
  109.         public string DirectoryPath 
  110.         { 
  111.             get 
  112.             { 
  113.                 return _DirectoryPath; 
  114.             } 
  115.             set 
  116.             { 
  117.                 _DirectoryPath = value; 
  118.             } 
  119.         } 
  120.   
  121.         /// <summary> 
  122.         /// FTP登录用户 
  123.         /// </summary> 
  124.         private string _UserName; 
  125.         /// <summary> 
  126.         /// FTP登录用户 
  127.         /// </summary> 
  128.         public string UserName 
  129.         { 
  130.             get 
  131.             { 
  132.                 return _UserName; 
  133.             } 
  134.             set 
  135.             { 
  136.                 _UserName = value; 
  137.             } 
  138.         } 
  139.   
  140.         /// <summary> 
  141.         /// 错误信息 
  142.         /// </summary> 
  143.         private string _ErrorMsg; 
  144.         /// <summary> 
  145.         /// 错误信息 
  146.         /// </summary> 
  147.         public string ErrorMsg 
  148.         { 
  149.             get 
  150.             { 
  151.                 return _ErrorMsg; 
  152.             } 
  153.             set 
  154.             { 
  155.                 _ErrorMsg = value; 
  156.             } 
  157.         } 
  158.   
  159.         /// <summary> 
  160.         /// FTP登录密码 
  161.         /// </summary> 
  162.         private string _Password; 
  163.         /// <summary> 
  164.         /// FTP登录密码 
  165.         /// </summary> 
  166.         public string Password 
  167.         { 
  168.             get 
  169.             { 
  170.                 return _Password; 
  171.             } 
  172.             set 
  173.             { 
  174.                 _Password = value; 
  175.             } 
  176.         } 
  177.   
  178.         /// <summary> 
  179.         /// 连接FTP服务器的代理服务 
  180.         /// </summary> 
  181.         private WebProxy _Proxy =
    null
  182.         /// <summary> 
  183.         /// 连接FTP服务器的代理服务 
  184.         /// </summary> 
  185.         public WebProxy Proxy 
  186.         { 
  187.             get 
  188.             { 
  189.                 return _Proxy; 
  190.             } 
  191.             set 
  192.             { 
  193.                 _Proxy = value; 
  194.             } 
  195.         } 
  196.   
  197.         /// <summary> 
  198.         /// 是否需要删除临时文件 
  199.         /// </summary> 
  200.         private bool _isDeleteTempFile =
    false
  201.         /// <summary> 
  202.         /// 异步上传所临时生成的文件 
  203.         /// </summary> 
  204.         private string _UploadTempFile =
    ""
  205.         #endregion 
  206.         #region 事件 
  207.         public delegate
    void De_DownloadProgressChanged(
    object sender, DownloadProgressChangedEventArgs e ); 
  208.         public delegate
    void De_DownloadDataCompleted( object sender, System.ComponentModel.AsyncCompletedEventArgs e ); 
  209.         public delegate
    void De_UploadProgressChanged( object sender, UploadProgressChangedEventArgs e ); 
  210.         public delegate
    void De_UploadFileCompleted( object sender, UploadFileCompletedEventArgs e ); 
  211.   
  212.         /// <summary> 
  213.         /// 异步下载进度发生改变触发的事件 
  214.         /// </summary> 
  215.         public event De_DownloadProgressChanged DownloadProgressChanged; 
  216.         /// <summary> 
  217.         /// 异步下载文件完成之后触发的事件 
  218.         /// </summary> 
  219.         public event De_DownloadDataCompleted DownloadDataCompleted; 
  220.         /// <summary> 
  221.         /// 异步上传进度发生改变触发的事件 
  222.         /// </summary> 
  223.         public event De_UploadProgressChanged UploadProgressChanged; 
  224.         /// <summary> 
  225.         /// 异步上传文件完成之后触发的事件 
  226.         /// </summary> 
  227.         public event De_UploadFileCompleted UploadFileCompleted; 
  228.         #endregion 
  229.         #region 构造析构函数 
  230.         /// <summary> 
  231.         /// 构造函数 
  232.         /// </summary> 
  233.         /// <param name="FtpUri">FTP地址</param> 
  234.         /// <param name="strUserName">登录用户名</param> 
  235.         /// <param name="strPassword">登录密码</param> 
  236.         public NewFtp( Uri FtpUri,
    string strUserName, string strPassword ) 
  237.         { 
  238.             this._Uri =
    new Uri( FtpUri.GetLeftPart( UriPartial.Authority ) ); 
  239.             _DirectoryPath = FtpUri.AbsolutePath; 
  240.             if( !_DirectoryPath.EndsWith(
    "/" ) ) 
  241.             { 
  242.                 _DirectoryPath += "/"
  243.             } 
  244.             this._UserName = strUserName; 
  245.             this._Password = strPassword; 
  246.             this._Proxy =
    null
  247.         } 
  248.         /// <summary> 
  249.         /// 构造函数 
  250.         /// </summary> 
  251.         /// <param name="FtpUri">FTP地址</param> 
  252.         /// <param name="strUserName">登录用户名</param> 
  253.         /// <param name="strPassword">登录密码</param> 
  254.         /// <param name="objProxy">连接代理</param> 
  255.         public NewFtp( Uri FtpUri,
    string strUserName, string strPassword, WebProxy objProxy ) 
  256.         { 
  257.             this._Uri = new Uri( FtpUri.GetLeftPart( UriPartial.Authority ) ); 
  258.             _DirectoryPath = FtpUri.AbsolutePath; 
  259.             if( !_DirectoryPath.EndsWith(
    "/" ) ) 
  260.             { 
  261.                 _DirectoryPath += "/"
  262.             } 
  263.             this._UserName = strUserName; 
  264.             this._Password = strPassword; 
  265.             this._Proxy = objProxy; 
  266.         } 
  267.         /// <summary> 
  268.         /// 构造函数 
  269.         /// </summary> 
  270.         public NewFtp() 
  271.         { 
  272.             this._UserName =
    "anonymous"//匿名用户 
  273.             this._Password =
    "@anonymous"
  274.             this._Uri =
    null
  275.             this._Proxy = null
  276.         } 
  277.   
  278.         /// <summary> 
  279.         /// 析构函数 
  280.         /// </summary> 
  281.         ~NewFtp() 
  282.         { 
  283.             if( Response != null
  284.             { 
  285.                 Response.Close(); 
  286.                 Response = null
  287.             } 
  288.             if( Request !=
    null
  289.             { 
  290.                 Request.Abort(); 
  291.                 Request = null
  292.             } 
  293.         } 
  294.         #endregion 
  295.         #region 建立连接 
  296.         /// <summary> 
  297.         /// 建立FTP链接,返回响应对象 
  298.         /// </summary> 
  299.         /// <param name="uri">FTP地址</param> 
  300.         /// <param name="FtpMathod">操作命令</param> 
  301.         private FtpWebResponse Open( Uri uri,
    string FtpMathod ) 
  302.         { 
  303.             try 
  304.             { 
  305.                 Request = ( FtpWebRequest ) WebRequest.Create( uri ); 
  306.                 Request.Method = FtpMathod; 
  307.                 Request.UseBinary = true
  308.                 Request.Credentials =
    new
    NetworkCredential( this.UserName,
    this.Password ); 
  309.                 if( this.Proxy !=
    null
  310.                 { 
  311.                     Request.Proxy = this.Proxy; 
  312.                 } 
  313.                 return ( FtpWebResponse ) Request.GetResponse(); 
  314.             } 
  315.             catch( Exception ep ) 
  316.             { 
  317.                 ErrorMsg = ep.ToString(); 
  318.                 throw ep; 
  319.             } 
  320.         } 
  321.         /// <summary> 
  322.         /// 建立FTP链接,返回请求对象 
  323.         /// </summary> 
  324.         /// <param name="uri">FTP地址</param> 
  325.         /// <param name="FtpMathod">操作命令</param> 
  326.         private FtpWebRequest OpenRequest( Uri uri,
    string FtpMathod ) 
  327.         { 
  328.             try 
  329.             { 
  330.                 Request = ( FtpWebRequest ) WebRequest.Create( uri ); 
  331.                 Request.Method = FtpMathod; 
  332.                 Request.UseBinary = true
  333.                 Request.Credentials = new NetworkCredential(
    this.UserName, this.Password ); 
  334.                 if(
    this.Proxy != null
  335.                 { 
  336.                     Request.Proxy = this.Proxy; 
  337.                 } 
  338.                 return Request; 
  339.             } 
  340.             catch( Exception ep ) 
  341.             { 
  342.                 ErrorMsg = ep.ToString(); 
  343.                 throw ep; 
  344.             } 
  345.         } 
  346.         #endregion 
  347.         #region 下载文件 
  348.   
  349.         /// <summary> 
  350.         /// 从FTP服务器下载文件,使用与远程文件同名的文件名来保存文件 
  351.         /// </summary> 
  352.         /// <param name="RemoteFileName">远程文件名</param> 
  353.         /// <param name="LocalPath">本地路径</param> 
  354.   
  355.         public bool DownloadFile(
    string RemoteFileName, string LocalPath ) 
  356.         { 
  357.             return DownloadFile( RemoteFileName, LocalPath, RemoteFileName ); 
  358.         } 
  359.         /// <summary> 
  360.         /// 从FTP服务器下载文件,指定本地路径和本地文件名 
  361.         /// </summary> 
  362.         /// <param name="RemoteFileName">远程文件名</param> 
  363.         /// <param name="LocalPath">本地路径</param> 
  364.         /// <param name="LocalFilePath">保存文件的本地路径,后面带有""</param> 
  365.         /// <param name="LocalFileName">保存本地的文件名</param> 
  366.         public bool DownloadFile(
    string RemoteFileName, string LocalPath,
    string LocalFileName ) 
  367.         { 
  368.             byte[] bt =
    null
  369.             try 
  370.             { 
  371.                 if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( LocalFileName ) || !IsValidPathChars( LocalPath ) ) 
  372.                 { 
  373.                     throw new Exception(
    "非法文件名或目录名!" ); 
  374.                 } 
  375.                 if( !Directory.Exists( LocalPath ) ) 
  376.                 { 
  377.                     throw new Exception(
    "本地文件路径不存在!" ); 
  378.                 } 
  379.   
  380.                 string LocalFullPath = Path.Combine( LocalPath, LocalFileName ); 
  381.                 if( File.Exists( LocalFullPath ) ) 
  382.                 { 
  383.                     throw new Exception(
    "当前路径下已经存在同名文件!" ); 
  384.                 } 
  385.                 bt = DownloadFile( RemoteFileName ); 
  386.                 if( bt !=
    null
  387.                 { 
  388.                     FileStream stream =
    new
    FileStream( LocalFullPath, FileMode.Create ); 
  389.                     stream.Write( bt, 0, bt.Length ); 
  390.                     stream.Flush(); 
  391.                     stream.Close(); 
  392.                     return
    true
  393.                 } 
  394.                 else 
  395.                 { 
  396.                     return
    false
  397.                 } 
  398.             } 
  399.             catch( Exception ep ) 
  400.             { 
  401.                 ErrorMsg = ep.ToString(); 
  402.                 throw ep; 
  403.             } 
  404.         } 
  405.   
  406.         /// <summary> 
  407.         /// 从FTP服务器下载文件,返回文件二进制数据 
  408.         /// </summary> 
  409.         /// <param name="RemoteFileName">远程文件名</param> 
  410.         public byte[] DownloadFile(
    string RemoteFileName ) 
  411.         { 
  412.             try 
  413.             { 
  414.                 if( !IsValidFileChars( RemoteFileName ) ) 
  415.                 { 
  416.                     throw
    new Exception( "非法文件名或目录名!" ); 
  417.                 } 
  418.                 Response = Open( new Uri(
    this.Uri.ToString() + RemoteFileName ), WebRequestMethods.Ftp.DownloadFile ); 
  419.                 Stream Reader = Response.GetResponseStream(); 
  420.   
  421.                 MemoryStream mem = new MemoryStream( 1024 * 500 ); 
  422.                 byte[] buffer =
    new byte[ 1024 ]; 
  423.                 int bytesRead = 0; 
  424.                 int TotalByteRead = 0; 
  425.                 while( true
  426.                 { 
  427.                     bytesRead = Reader.Read( buffer, 0, buffer.Length ); 
  428.                     TotalByteRead += bytesRead; 
  429.                     if( bytesRead == 0 ) 
  430.                         break
  431.                     mem.Write( buffer, 0, bytesRead ); 
  432.                 } 
  433.                 if( mem.Length > 0 ) 
  434.                 { 
  435.                     return mem.ToArray(); 
  436.                 } 
  437.                 else 
  438.                 { 
  439.                     return null
  440.                 } 
  441.             } 
  442.             catch( Exception ep ) 
  443.             { 
  444.                 ErrorMsg = ep.ToString(); 
  445.                 throw ep; 
  446.             } 
  447.         } 
  448.         #endregion 
  449.         #region 异步下载文件 
  450.         /// <summary> 
  451.         /// 从FTP服务器异步下载文件,指定本地路径和本地文件名 
  452.         /// </summary> 
  453.         /// <param name="RemoteFileName">远程文件名</param>      
     
  454.         /// <param name="LocalPath">保存文件的本地路径,后面带有""</param> 
  455.         /// <param name="LocalFileName">保存本地的文件名</param> 
  456.         public void DownloadFileAsync(
    string RemoteFileName, string LocalPath,
    string LocalFileName ) 
  457.         { 
  458.             byte[] bt =
    null
  459.             try 
  460.             { 
  461.                 if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( LocalFileName ) || !IsValidPathChars( LocalPath ) ) 
  462.                 { 
  463.                     throw new Exception(
    "非法文件名或目录名!" ); 
  464.                 } 
  465.                 if( !Directory.Exists( LocalPath ) ) 
  466.                 { 
  467.                     throw new Exception(
    "本地文件路径不存在!" ); 
  468.                 } 
  469.   
  470.                 string LocalFullPath = Path.Combine( LocalPath, LocalFileName ); 
  471.                 if( File.Exists( LocalFullPath ) ) 
  472.                 { 
  473.                     throw new Exception(
    "当前路径下已经存在同名文件!" ); 
  474.                 } 
  475.                 DownloadFileAsync( RemoteFileName, LocalFullPath ); 
  476.   
  477.             } 
  478.             catch( Exception ep ) 
  479.             { 
  480.                 ErrorMsg = ep.ToString(); 
  481.                 throw ep; 
  482.             } 
  483.         } 
  484.   
  485.         /// <summary> 
  486.         /// 从FTP服务器异步下载文件,指定本地完整路径文件名 
  487.         /// </summary> 
  488.         /// <param name="RemoteFileName">远程文件名</param> 
  489.         /// <param name="LocalFullPath">本地完整路径文件名</param> 
  490.         public void DownloadFileAsync(
    string RemoteFileName, string LocalFullPath ) 
  491.         { 
  492.             try 
  493.             { 
  494.                 if( !IsValidFileChars( RemoteFileName ) ) 
  495.                 { 
  496.                     throw
    new Exception( "非法文件名或目录名!" ); 
  497.                 } 
  498.                 if( File.Exists( LocalFullPath ) ) 
  499.                 { 
  500.                     throw
    new Exception( "当前路径下已经存在同名文件!" ); 
  501.                 } 
  502.                 MyWebClient client =
    new
    MyWebClient(); 
  503.   
  504.                 client.DownloadProgressChanged +=
    new
    DownloadProgressChangedEventHandler( client_DownloadProgressChanged ); 
  505.                 client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler( client_DownloadFileCompleted ); 
  506.                 client.Credentials =
    new
    NetworkCredential( this.UserName,
    this.Password ); 
  507.                 if( this.Proxy !=
    null
  508.                 { 
  509.                     client.Proxy = this.Proxy; 
  510.                 } 
  511.                 client.DownloadFileAsync( new Uri(
    this.Uri.ToString() + RemoteFileName ), LocalFullPath ); 
  512.             } 
  513.             catch( Exception ep ) 
  514.             { 
  515.                 ErrorMsg = ep.ToString(); 
  516.                 throw ep; 
  517.             } 
  518.         } 
  519.   
  520.         /// <summary> 
  521.         /// 异步下载文件完成之后触发的事件 
  522.         /// </summary> 
  523.         /// <param name="sender">下载对象</param> 
  524.         /// <param name="e">数据信息对象</param> 
  525.         void client_DownloadFileCompleted(
    object sender, System.ComponentModel.AsyncCompletedEventArgs e ) 
  526.         { 
  527.             if( DownloadDataCompleted !=
    null
  528.             { 
  529.                 DownloadDataCompleted( sender, e ); 
  530.             } 
  531.         } 
  532.   
  533.         /// <summary> 
  534.         /// 异步下载进度发生改变触发的事件 
  535.         /// </summary> 
  536.         /// <param name="sender">下载对象</param> 
  537.         /// <param name="e">进度信息对象</param> 
  538.         void client_DownloadProgressChanged(
    object sender, DownloadProgressChangedEventArgs e ) 
  539.         { 
  540.             if( DownloadProgressChanged !=
    null
  541.             { 
  542.                 DownloadProgressChanged( sender, e ); 
  543.             } 
  544.         } 
  545.         #endregion 
  546.         #region 上传文件 
  547.         /// <summary> 
  548.         /// 上传文件到FTP服务器 
  549.         /// </summary> 
  550.         /// <param name="LocalFullPath">本地带有完整路径的文件名</param> 
  551.         public bool UploadFile(
    string LocalFullPath ) 
  552.         { 
  553.             return UploadFile( LocalFullPath, Path.GetFileName( LocalFullPath ),
    false ); 
  554.         } 
  555.         /// <summary> 
  556.         /// 上传文件到FTP服务器 
  557.         /// </summary> 
  558.         /// <param name="LocalFullPath">本地带有完整路径的文件</param> 
  559.         /// <param name="OverWriteRemoteFile">是否覆盖远程服务器上面同名的文件</param> 
  560.         public bool UploadFile(
    string LocalFullPath, bool OverWriteRemoteFile ) 
  561.         { 
  562.             return UploadFile( LocalFullPath, Path.GetFileName( LocalFullPath ), OverWriteRemoteFile ); 
  563.         } 
  564.         /// <summary> 
  565.         /// 上传文件到FTP服务器 
  566.         /// </summary> 
  567.         /// <param name="LocalFullPath">本地带有完整路径的文件</param> 
  568.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  569.         public bool UploadFile(
    string LocalFullPath, string RemoteFileName ) 
  570.         { 
  571.             return UploadFile( LocalFullPath, RemoteFileName,
    false ); 
  572.         } 
  573.         /// <summary> 
  574.         /// 上传文件到FTP服务器 
  575.         /// </summary> 
  576.         /// <param name="LocalFullPath">本地带有完整路径的文件名</param> 
  577.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  578.         /// <param name="OverWriteRemoteFile">是否覆盖远程服务器上面同名的文件</param> 
  579.         public bool UploadFile(
    string LocalFullPath, string RemoteFileName,
    bool

抱歉!评论已关闭.