FTP上传类FTP上传类
2013年08月15日
⁄ 综合
⁄ 共 16447字 ⁄ 字号
小 中 大
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Globalization;
- using System.Text.RegularExpressions;
- namespace WebBaseLib
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #region 文件信息结构
- public struct FileStruct
- {
- public string Flags;
- public string Owner;
- public string Group;
- public bool IsDirectory;
- public DateTime CreateTime;
- public string Name;
- }
- public enum FileListStyle
- {
- UnixStyle,
- WindowsStyle,
- Unknown
- }
- #endregion
- public class NewFtp
- {
- #region 属性信息
-
-
-
- FtpWebRequest Request = null;
-
-
-
- FtpWebResponse Response = null;
-
-
-
- private Uri _Uri;
-
-
-
- public Uri Uri
- {
- get
- {
- if( _DirectoryPath ==
"/" )
- {
- return _Uri;
- }
- else
- {
- string strUri = _Uri.ToString();
- if( strUri.EndsWith(
"/" ) )
- {
- strUri = strUri.Substring( 0, strUri.Length - 1 );
- }
- return new Uri( strUri +
this.DirectoryPath );
- }
- }
- set
- {
- if( value.Scheme != Uri.UriSchemeFtp )
- {
- throw
new Exception( "Ftp 地址格式错误!" );
- }
- _Uri = new Uri( value.GetLeftPart( UriPartial.Authority ) );
- _DirectoryPath = value.AbsolutePath;
- if( !_DirectoryPath.EndsWith(
"/" ) )
- {
- _DirectoryPath +=
"/";
- }
- }
- }
-
-
-
-
- private string _DirectoryPath;
-
-
-
-
- public string DirectoryPath
- {
- get
- {
- return _DirectoryPath;
- }
- set
- {
- _DirectoryPath = value;
- }
- }
-
-
-
-
- private string _UserName;
-
-
-
- public string UserName
- {
- get
- {
- return _UserName;
- }
- set
- {
- _UserName = value;
- }
- }
-
-
-
-
- private string _ErrorMsg;
-
-
-
- public string ErrorMsg
- {
- get
- {
- return _ErrorMsg;
- }
- set
- {
- _ErrorMsg = value;
- }
- }
-
-
-
-
- private string _Password;
-
-
-
- public string Password
- {
- get
- {
- return _Password;
- }
- set
- {
- _Password = value;
- }
- }
-
-
-
-
- private WebProxy _Proxy =
null;
-
-
-
- public WebProxy Proxy
- {
- get
- {
- return _Proxy;
- }
- set
- {
- _Proxy = value;
- }
- }
-
-
-
-
- private bool _isDeleteTempFile =
false;
-
-
-
- private string _UploadTempFile =
"";
- #endregion
- #region 事件
- public delegate
void De_DownloadProgressChanged(
object sender, DownloadProgressChangedEventArgs e );
- public delegate
void De_DownloadDataCompleted( object sender, System.ComponentModel.AsyncCompletedEventArgs e );
- public delegate
void De_UploadProgressChanged( object sender, UploadProgressChangedEventArgs e );
- public delegate
void De_UploadFileCompleted( object sender, UploadFileCompletedEventArgs e );
-
-
-
-
- public event De_DownloadProgressChanged DownloadProgressChanged;
-
-
-
- public event De_DownloadDataCompleted DownloadDataCompleted;
-
-
-
- public event De_UploadProgressChanged UploadProgressChanged;
-
-
-
- public event De_UploadFileCompleted UploadFileCompleted;
- #endregion
- #region 构造析构函数
-
-
-
-
-
-
- public NewFtp( Uri FtpUri,
string strUserName, string strPassword )
- {
- this._Uri =
new Uri( FtpUri.GetLeftPart( UriPartial.Authority ) );
- _DirectoryPath = FtpUri.AbsolutePath;
- if( !_DirectoryPath.EndsWith(
"/" ) )
- {
- _DirectoryPath += "/";
- }
- this._UserName = strUserName;
- this._Password = strPassword;
- this._Proxy =
null;
- }
-
-
-
-
-
-
-
- public NewFtp( Uri FtpUri,
string strUserName, string strPassword, WebProxy objProxy )
- {
- this._Uri = new Uri( FtpUri.GetLeftPart( UriPartial.Authority ) );
- _DirectoryPath = FtpUri.AbsolutePath;
- if( !_DirectoryPath.EndsWith(
"/" ) )
- {
- _DirectoryPath += "/";
- }
- this._UserName = strUserName;
- this._Password = strPassword;
- this._Proxy = objProxy;
- }
-
-
-
- public NewFtp()
- {
- this._UserName =
"anonymous";
- this._Password =
"@anonymous";
- this._Uri =
null;
- this._Proxy = null;
- }
-
-
-
-
- ~NewFtp()
- {
- if( Response != null )
- {
- Response.Close();
- Response = null;
- }
- if( Request !=
null )
- {
- Request.Abort();
- Request = null;
- }
- }
- #endregion
- #region 建立连接
-
-
-
-
-
- private FtpWebResponse Open( Uri uri,
string FtpMathod )
- {
- try
- {
- Request = ( FtpWebRequest ) WebRequest.Create( uri );
- Request.Method = FtpMathod;
- Request.UseBinary = true;
- Request.Credentials =
new NetworkCredential( this.UserName,
this.Password );
- if( this.Proxy !=
null )
- {
- Request.Proxy = this.Proxy;
- }
- return ( FtpWebResponse ) Request.GetResponse();
- }
- catch( Exception ep )
- {
- ErrorMsg = ep.ToString();
- throw ep;
- }
- }
-
-
-
-
-
- private FtpWebRequest OpenRequest( Uri uri,
string FtpMathod )
- {
- try
- {
- Request = ( FtpWebRequest ) WebRequest.Create( uri );
- Request.Method = FtpMathod;
- Request.UseBinary = true;
- Request.Credentials = new NetworkCredential(
this.UserName, this.Password );
- if(
this.Proxy != null )
- {
- Request.Proxy = this.Proxy;
- }
- return Request;
- }
- catch( Exception ep )
- {
- ErrorMsg = ep.ToString();
- throw ep;
- }
- }
- #endregion
- #region 下载文件
-
-
-
-
-
-
-
- public bool DownloadFile(
string RemoteFileName, string LocalPath )
- {
- return DownloadFile( RemoteFileName, LocalPath, RemoteFileName );
- }
-
-
-
-
-
-
-
- public bool DownloadFile(
string RemoteFileName, string LocalPath,
string LocalFileName )
- {
- byte[] bt =
null;
- try
- {
- if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( LocalFileName ) || !IsValidPathChars( LocalPath ) )
- {
- throw new Exception(
"非法文件名或目录名!" );
- }
- if( !Directory.Exists( LocalPath ) )
- {
- throw new Exception(
"本地文件路径不存在!" );
- }
-
- string LocalFullPath = Path.Combine( LocalPath, LocalFileName );
- if( File.Exists( LocalFullPath ) )
- {
- throw new Exception(
"当前路径下已经存在同名文件!" );
- }
- bt = DownloadFile( RemoteFileName );
- if( bt !=
null )
- {
- FileStream stream =
new FileStream( LocalFullPath, FileMode.Create );
- stream.Write( bt, 0, bt.Length );
- stream.Flush();
- stream.Close();
- return
true;
- }
- else
- {
- return
false;
- }
- }
- catch( Exception ep )
- {
- ErrorMsg = ep.ToString();
- throw ep;
- }
- }
-
-
-
-
-
- public byte[] DownloadFile(
string RemoteFileName )
- {
- try
- {
- if( !IsValidFileChars( RemoteFileName ) )
- {
- throw
new Exception( "非法文件名或目录名!" );
- }
- Response = Open( new Uri(
this.Uri.ToString() + RemoteFileName ), WebRequestMethods.Ftp.DownloadFile );
- Stream Reader = Response.GetResponseStream();
-
- MemoryStream mem = new MemoryStream( 1024 * 500 );
- byte[] buffer =
new byte[ 1024 ];
- int bytesRead = 0;
- int TotalByteRead = 0;
- while( true )
- {
- bytesRead = Reader.Read( buffer, 0, buffer.Length );
- TotalByteRead += bytesRead;
- if( bytesRead == 0 )
- break;
- mem.Write( buffer, 0, bytesRead );
- }
- if( mem.Length > 0 )
- {
- return mem.ToArray();
- }
- else
- {
- return null;
- }
- }
- catch( Exception ep )
- {
- ErrorMsg = ep.ToString();
- throw ep;
- }
- }
- #endregion
- #region 异步下载文件
-
-
-
-
-
-
- public void DownloadFileAsync(
string RemoteFileName, string LocalPath,
string LocalFileName )
- {
- byte[] bt =
null;
- try
- {
- if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( LocalFileName ) || !IsValidPathChars( LocalPath ) )
- {
- throw new Exception(
"非法文件名或目录名!" );
- }
- if( !Directory.Exists( LocalPath ) )
- {
- throw new Exception(
"本地文件路径不存在!" );
- }
-
- string LocalFullPath = Path.Combine( LocalPath, LocalFileName );
- if( File.Exists( LocalFullPath ) )
- {
- throw new Exception(
"当前路径下已经存在同名文件!" );
- }
- DownloadFileAsync( RemoteFileName, LocalFullPath );
-
- }
- catch( Exception ep )
- {
- ErrorMsg = ep.ToString();
- throw ep;
- }
- }
-
-
-
-
-
-
- public void DownloadFileAsync(
string RemoteFileName, string LocalFullPath )
- {
- try
- {
- if( !IsValidFileChars( RemoteFileName ) )
- {
- throw
new Exception( "非法文件名或目录名!" );
- }
- if( File.Exists( LocalFullPath ) )
- {
- throw
new Exception( "当前路径下已经存在同名文件!" );
- }
- MyWebClient client =
new MyWebClient();
-
- client.DownloadProgressChanged +=
new DownloadProgressChangedEventHandler( client_DownloadProgressChanged );
- client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler( client_DownloadFileCompleted );
- client.Credentials =
new NetworkCredential( this.UserName,
this.Password );
- if( this.Proxy !=
null )
- {
- client.Proxy = this.Proxy;
- }
- client.DownloadFileAsync( new Uri(
this.Uri.ToString() + RemoteFileName ), LocalFullPath );
- }
- catch( Exception ep )
- {
- ErrorMsg = ep.ToString();
- throw ep;
- }
- }
-
-
-
-
-
-
- void client_DownloadFileCompleted(
object sender, System.ComponentModel.AsyncCompletedEventArgs e )
- {
- if( DownloadDataCompleted !=
null )
- {
- DownloadDataCompleted( sender, e );
- }
- }
-
-
-
-
-
-
- void client_DownloadProgressChanged(
object sender, DownloadProgressChangedEventArgs e )
- {
- if( DownloadProgressChanged !=
null )
- {
- DownloadProgressChanged( sender, e );
- }
- }
- #endregion
- #region 上传文件
-
-
-
-
- public bool UploadFile(
string LocalFullPath )
- {
- return UploadFile( LocalFullPath, Path.GetFileName( LocalFullPath ),
false );
- }
-
-
-
-
-
- public bool UploadFile(
string LocalFullPath, bool OverWriteRemoteFile )
- {
- return UploadFile( LocalFullPath, Path.GetFileName( LocalFullPath ), OverWriteRemoteFile );
- }
-
-
-
-
-
- public bool UploadFile(
string LocalFullPath, string RemoteFileName )
- {
- return UploadFile( LocalFullPath, RemoteFileName,
false );
- }
-
-
-
-
-
-
- public bool UploadFile(
string LocalFullPath, string RemoteFileName,
bool