win7搭建ftp服務器及文件上傳


參考
http://www.cnblogs.com/yangpaopao/archive/2010/07/30/1788513.html

1、到控制面板---程序---打開或關閉windows功能,列表內找到Internet信息服務(展開)---選中FTP的三個項

 

2、到控制面板---系統和安全---管理工具---Internet 信息服務(IIS)管理器---右鍵點你計算機名稱那里,選擇添加FTP站點

 

 

3、FTP站點名稱輸入:"localhost"---選擇你的FTP目錄物理路徑,點下一步---Ip地址選“自己的IP”,端口可以自己設,勾上“自動FTP站點”,SSL選“允許”

點下一步---身份驗證選“匿名”,允許訪問選“匿名用戶”,權限勾“讀取”或“寫入”,點完成。

 

 

4、到控制面板---系統和安全---允許程序通過防火牆---鈎上FTP及后面兩個框框。

 

現在在WIN7系統 下通過自帶的IIS搭建的匿名FIP就已成功,我們可以通過輸入FTP的地址來進行數據的上傳與下載。

文件上傳函數
  public static int UploadFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword)
        {

            FileInfo fileInf = new FileInfo(filePath + "\\" + filename);
            string uri = "ftp://" + ftpServerIP;
            FtpWebRequest reqFTP;

            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
            try
            {
                // Provide the WebPermission Credintials
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.UsePassive = false; 
                // By default KeepAlive is true, where the control connection is not closed
                // after a command is executed.
                reqFTP.KeepAlive = false;

                // Specify the command to be executed.
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

                // Specify the data transfer type.
                reqFTP.UseBinary = true;

                // Notify the server about the size of the uploaded file
                reqFTP.ContentLength = fileInf.Length;

                // The buffer size is set to 2kb
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;

                // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
                //FileStream fs = fileInf.OpenRead();
                FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                // Stream to which the file to be upload is written
                Stream strm = reqFTP.GetRequestStream();

                // Read from the file stream 2kb at a time
                contentLen = fs.Read(buff, 0, buffLength);

                // Till Stream content ends
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                // Close the file stream and the Request Stream
                strm.Close();
                fs.Close();
                return 0;
            }
            catch (Exception ex)
            {
                reqFTP.Abort();
                return -2;
            }
        }
調用: UploadFtp("D:\\", "temp.txt", "192.168.1.106:2121", "anonymous", "111");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM