本文使用.net 2.0(c#)来实现一般的FTP功能介绍微软的.net framework 2.0相对于1.x来说增加了对FTP的支持。以前为了符合我的需求,我不等不使用第三方类库来实现FTP功能,但是为了可靠,还是使用.net framework的类比较好。我的这段代码没有做成可重复使用的类库的形式,但它却是比较容易理解的并能满足你的需求。它可以实现上传,下载,删除等任意功能。在这篇文章的后面将给大家出示.net 2.0下实现ftp的简单代码,使用的语言是c#。或许是因为这是.net新增的类,又或许是第三方类库已经能很好的实现你的需求,.net 2.0的这部分类库并没有得到足够的关注。背景作为我的工作的一部分,我已经使用了ftp模块,但是我只能在.net 1.1中去使用它,所以我不能深入的研究.net 2.0下ftp的实现。但是我相信,.ne 2.0下对ftp的支持是非常好的。  代码不要忘记引入命名空间using System.Net;using System.IO;下面的几个步骤包括了使用FtpWebRequest类实现ftp功能的一般过程 1、创建一个FtpWebRequest对象,指向ftp服务器的uri2、设置ftp的执行方法(上传,下载等)3、给FtpWebRequest对象设置属性(是否支持ssl,是否使用二进制传输等)4、设置登录验证(用户名,密码)5、执行请求6、接收相应流(如果需要的话)7、如果没有打开的流,则关闭ftp请求 开发任何ftp应用程序都需要一个相关的ftp服务器及它的配置信息。FtpWebRequest暴露了一些属性来设置这些信息。 接下来的代码示例了上传功能 首先设置一个uri地址,包括路径和文件名。这个uri被使用在FtpWebRequest实例中。 然后根据ftp请求设置FtpWebRequest对象的属性 其中一些重要的属性如下:    ·Credentials - 指定登录ftp服务器的用户名和密码。    ·KeepAlive - 指定连接是应该关闭还是在请求完成之后关闭,默认为true    ·UseBinary - 指定文件传输的类型。有两种文件传输模式,一种是Binary,另一种是ASCII。两种方法在传输时,字节的第8位是不同的。ASCII使用第8位作为错误控制,而Binary的8位都是有意义的。所以当你使用ASCII传输时要小心一些。简单的说,如果能用记事本读和写的文件用ASCII传输就是安全的,而其他的则必须使用Binary模式。当然使用Binary模式发送ASCII文件也是非常好的。    ·UsePassive - 指定使用主动模式还是被动模式。早先所有客户端都使用主动模式,而且工作的很好,而现在因为客户端防火墙的存在,将会关闭一些端口,这样主动模式将会失败。在这种情况下就要使用被动模式,但是一些端口也可能被服务器的防火墙封掉。不过因为ftp服务器需要它的ftp服务连接到一定数量的客户端,所以他们总是支持被动模式的。这就是我们为什么要使用被动模式的原意,为了确保数据可以正确的传输,使用被动模式要明显优于主动模式。(译者注:主动(PORT)模式建立数据传输通道是由服务器端发起的,服务器使用20端口连接客户端的某一个大于1024的端口;在被动(PASV)模式中,数据传输的通道的建立是由FTP客户端发起的,他使用一个大于1024的端口连接服务器的1024以上的某一个端口)    ·ContentLength - 设置这个属性对于ftp服务器是有用的,但是客户端不使用它,因为FtpWebRequest忽略这个属性,所以在这种情况下,该属性是无效的。但是如果我们设置了这个属性,ftp服务器将会提前预知文件的大小(在upload时会有这种情况)    ·Method - 指定当前请求是什么命令(upload,download,filelist等)。这个值定义在结构体WebRequestMethods.Ftp中。 private void Upload(string filename){    FileInfo fileInf = new FileInfo(filename);    string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;    FtpWebRequest reqFTP;      // 根据uri创建FtpWebRequest对象     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));      // ftp用户名和密码    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);      // 默认为true,连接不会被关闭    // 在一个命令之后被执行    reqFTP.KeepAlive = false;      // 指定执行什么命令    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;      // 指定数据传输类型    reqFTP.UseBinary = true;      // 上传文件时通知服务器文件的大小    reqFTP.ContentLength = fileInf.Length;      // 缓冲大小设置为2kb    int buffLength = 2048;     byte[] buff = new byte[buffLength];    int contentLen;      // 打开一个文件流 (System.IO.FileStream) 去读上传的文件    FileStream fs = fileInf.OpenRead();     try    {        // 把上传的文件写入流        Stream strm = reqFTP.GetRequestStream();          // 每次读文件流的2kb        contentLen = fs.Read(buff, 0, buffLength);          // 流内容没有结束        while (contentLen != 0)        {            // 把内容从file stream 写入 upload stream            strm.Write(buff, 0, contentLen);             contentLen = fs.Read(buff, 0, buffLength);        }          // 关闭两个流        strm.Close();        fs.Close();    }    catch (Exception ex)    {        MessageBox.Show(ex.Message, "Upload Error");    }}以上代码简单的示例了ftp的上传功能。创建一个指向某ftp服务器的FtpWebRequest对象,然后设置其不同的属性Credentials,KeepAlive,Method,UseBinary,ContentLength。 打开本地机器上的文件,把其内容写入ftp请求流。缓冲的大小为2kb,无论上传大文件还是小文件,这都是一个合适的大小。 private 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));         reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;         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();    }    catch (Exception ex)    {        MessageBox.Show(ex.Message);    }}上面的代码实现了从ftp服务器上下载文件的功能。这不同于之前所提到的上传功能,下载需要一个响应流,它包含着下载文件的内容。这个下载的文件是在FtpWebRequest对象中的uri指定的。在得到所请求的文件后,通过FtpWebRequest对象的GetResponse()方法下载文件。它将把文件作为一个流下载到你的客户端的机器上。 注意:我们可以设置文件在我们本地机器上的存放路径和名称。 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(ftpUserID, ftpPassword);                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();                }                // to remove the trailing '\n'                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);                downloadFiles = null;                return downloadFiles;        }}上面的代码示例了如何从ftp服务器上获得文件列表。uri指向ftp服务器的地址。我们使用StreamReader对象来存储一个流,文件名称列表通过“\r\n”分隔开,也就是说每一个文件名称都占一行。你可以使用StreamReader对象的ReadToEnd()方法来得到文件列表。上面的代码中我们用一个StringBuilder对象来保存文件名称,然后把结果通过分隔符分开后作为一个数组返回。我确定只是一个比较好的方法。 其他的实现如Rename,Delete,GetFileSize,FileListDetails,MakeDir等与上面的几段代码类似,就不多说了。 注意:实现重命名的功能时,要把新的名字设置给FtpWebRequest对象的RenameTo属性。连接指定目录的时候,需要在FtpWebRequest对象所使用的uri中指明。

解决方案 »

  1.   

    需要注意的地方你在编码时需要注意以下几点:    ·除非EnableSsl属性被设置成true,否作所有数据,包括你的用户名和密码都将明文发给服务器,任何监视网络的人都可以获取到你连接服务器的验证信息。如果你连接的ftp服务器提供了SSL,你就应当把EnableSsl属性设置为true。    ·如果你没有访问ftp服务器的权限,将会抛出SecurityException错误·发送请求到ftp服务器需要调用GetResponse方法。当请求的操作完成后,一个FtpWebResponse对象将返回。这个FtpWebResponse对象提供了操作的状态和已经从ftp服务器上下载的数据。FtpWebResponse对象的StatusCode属性提供了ftp服务器返回的最后的状态代码。FtpWebResponse对象的StatusDescription属性为这个状态代码的描述。             MSDN:  using System;using System.Net;using System.Threading; using System.IO;namespace Examples.System.Net{    public class FtpState    {        private ManualResetEvent wait;        private FtpWebRequest request;        private string fileName;        private Exception operationException = null;        string status;                public FtpState()        {            wait = new ManualResetEvent(false);        }                public ManualResetEvent OperationComplete        {            get {return wait;}        }                public FtpWebRequest Request        {            get {return request;}            set {request = value;}        }                public string FileName        {            get {return fileName;}            set {fileName = value;}        }        public Exception OperationException        {            get {return operationException;}            set {operationException = value;}        }        public string StatusDescription        {            get {return status;}            set {status = value;}        }    }    public class AsynchronousFtpUpLoader    {          // Command line arguments are two strings:        // 1. The url that is the name of the file being uploaded to the server.        // 2. The name of the file on the local machine.        //        public static void Main(string[] args)        {            // Create a Uri instance with the specified URI string.            // If the URI is not correctly formed, the Uri constructor            // will throw an exception.            ManualResetEvent waitObject;                        Uri target = new Uri (args[0]);            string fileName = args[1];            FtpState state = new FtpState();            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);            request.Method = WebRequestMethods.Ftp.UploadFile;                        // This example uses anonymous logon.            // The request is anonymous by default; the credential does not have to be specified.             // The example specifies the credential only to            // control how actions are logged on the server.                        request.Credentials = new NetworkCredential ("anonymous","[email protected]");                        // Store the request in the object that we pass into the            // asynchronous operations.            state.Request = request;            state.FileName = fileName;                        // Get the event to wait on.            waitObject = state.OperationComplete;                        // Asynchronously get the stream for the file contents.            request.BeginGetRequestStream(                new AsyncCallback (EndGetStreamCallback),                 state            );                        // Block the current thread until all operations are complete.            waitObject.WaitOne();                        // The operations either completed or threw an exception.            if (state.OperationException != null)            {                throw state.OperationException;            }            else            {                Console.WriteLine("The operation completed - {0}", state.StatusDescription);            }        }        private static void EndGetStreamCallback(IAsyncResult ar)        {            FtpState state = (FtpState) ar.AsyncState;                        Stream requestStream = null;            // End the asynchronous call to get the request stream.            try            {                requestStream = state.Request.EndGetRequestStream(ar);                // Copy the file contents to the request stream.                const int bufferLength = 2048;                byte[] buffer = new byte[bufferLength];                int count = 0;                int readBytes = 0;                FileStream stream = File.OpenRead(state.FileName);                do                {                    readBytes = stream.Read(buffer, 0, bufferLength);                    requestStream.Write(buffer, 0, readBytes);                    count += readBytes;                }                while (readBytes != 0);                Console.WriteLine ("Writing {0} bytes to the stream.", count);                // IMPORTANT: Close the request stream before sending the request.                requestStream.Close();                // Asynchronously get the response to the upload request.                state.Request.BeginGetResponse(                    new AsyncCallback (EndGetResponseCallback),                     state                );            }             // Return exceptions to the main application thread.            catch (Exception e)            {                Console.WriteLine("Could not get the request stream.");                state.OperationException = e;                state.OperationComplete.Set();                return;            }                   }                // The EndGetResponseCallback method          // completes a call to BeginGetResponse.        private static void EndGetResponseCallback(IAsyncResult ar)        {            FtpState state = (FtpState) ar.AsyncState;            FtpWebResponse response = null;            try             {                response = (FtpWebResponse) state.Request.EndGetResponse(ar);                response.Close();                state.StatusDescription = response.StatusDescription;                // Signal the main application thread that                 // the operation is complete.                state.OperationComplete.Set();            }            // Return exceptions to the main application thread.            catch (Exception e)            {                Console.WriteLine ("Error getting response.");                state.OperationException = e;                state.OperationComplete.Set();            }        }    }}
      

  2.   

    参考一下这个:http://blog.csdn.net/yanleigis/archive/2007/11/20/1894881.aspx
      

  3.   


    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Net;
    using System.IO;
    using System.Text;namespace ftp
    {
        class ftp
        {
            public static bool ftp_Upload(string FtpURL, string username, string password, string FileName)
            {            string strTemp = FileName;
                strTemp = strTemp.Substring(strTemp.LastIndexOf(@"\") + 1);//截取文件名 
                Stream requestStream = null;
                FileStream fileStream = null;
                FtpWebResponse uploadResponse = null;
                try
                {
                    string uu = "ftp://" + FtpURL + strTemp;
                    FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uu);//创建完整的FTP服务地址
                    uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;  //设置FTP服务的操作为上载文件  
                    uploadRequest.Proxy = null;     //服务器通信代理
                    //设置用户名和密码
                    uploadRequest.Credentials = new NetworkCredential(username, password);//用户名和密码
                    requestStream = uploadRequest.GetRequestStream();
                    fileStream = File.Open(FileName, FileMode.Open);//打开指定路径的文件
                    byte[] buffer = new byte[4096];
                    int nRead;
                    while (true)//读取已打开文件的字节流写入缓冲区直至读取完毕
                    {
                        nRead = fileStream.Read(buffer, 0, buffer.Length);
                        if (nRead == 0)
                            break;
                        requestStream.Write(buffer, 0, buffer.Length);
                    }
                    fileStream.Close();
                    requestStream.Close();
                    uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();//服务器返回响应
                }
                catch (Exception e)
                {
                    MainForm.AddViewListValue("上传ftp失败" + e.Message, 5);
                    return false;
                }
                finally
                {
                    if (uploadResponse != null)
                        uploadResponse.Close();
                    if (fileStream != null)
                        fileStream.Close();
                    if (requestStream != null)
                        requestStream.Close();
                }
                return true;
            }
        }
    }
    调用:
    ftp.ftp_Upload("172.16.1.220:21/Test","test","221638","F:\Test:\1.txt");
      

  4.   

    参考一下这个: http://blog.csdn.net/yanleigis/archive/2007/11/20/1894881.aspx
      

  5.   

    简直是天才,在return true前加上一句messagebox.show("上传成功")
      

  6.   


    public class FTPManage
        {
            private FtpWebRequest ftpRequest = null;
            private string userName = string.Empty;//账户
            private string userPwd = string.Empty;//密码
            private string ftpServerIP = string.Empty;//服务器IP
            public FTPManage(string userName, string userPwd, string ftpServerIP)
            {
                this.userName = userName;
                this.userPwd = userPwd;
                this.ftpServerIP = ftpServerIP;
            }
     /// <summary>
            /// 上传文件
            /// </summary>
            /// <param name="ftpFilePath">在FTP主目录的路径</param>
            /// <param name="upLoadFilePath">需要上传的文件本地路径</param>
            public void UpLoadIngFile(string ftpFilePath, string upLoadFilePath)
            {//上传文件
                FileInfo upLoadFileInfo = new FileInfo(upLoadFilePath);//要上传的文件
                int buffLength = (int)upLoadFileInfo.Length;//上传文件数据流大小
                ftpRequest = null;
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpFilePath));
                FTPUserLogin();
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                ftpRequest.UseBinary = true;            ftpRequest.ContentLength = buffLength;
                byte[] buff = new byte[buffLength];
                int contentLen;
                FileStream fileStr = upLoadFileInfo.OpenRead();
                try
                {
                    Stream strm = ftpRequest.GetRequestStream();
                    contentLen = fileStr.Read(buff, 0, buffLength);
                    while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);
                        contentLen = fileStr.Read(buff, 0, buffLength);
                    }
                    strm.Close();
                    fileStr.Close();
                }
                catch
                {
                   // throw new Exception("上传文件失败");
                }
            }
      

  7.   


    忘记了应该把关闭IO流的代码写在finally里面,要不异常了流没关掉。要不用using
      

  8.   

    http://download.csdn.net/detail/fanbingyuan/2926731