為了測試我們寫的代碼是否具有ftp上傳下載功能,我們需要去下載一個虛擬FTP服務器,如:Serv-U。配置好Serv-U後,我們就可以編碼測試了。
引入命名空間:using System.Net;
////// 下載文件 /// /// 文件下載後存放路徑 /// 文件下載後的名稱 /// 要下載的文件名 ///private int Download(string filePath, string SaveFileName, string parFileName) { FtpWebRequest reqFTP; FileStream outputStream = new FileStream(filePath + "\\" + SaveFileName, FileMode.Create); try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strFTPIP + "/" + strFtpDownPath + "/" + parFileName)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(strFTPUser, strFTPPSW); 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); } ////下載成功後,刪除服務器上的文件。 //FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + strFTPIP + "/" + strFtpPath + "/" + fileName)); //request.Method = WebRequestMethods.Ftp.DeleteFile; //FtpWebResponse responseD = (FtpWebResponse)request.GetResponse(); ftpStream.Close(); outputStream.Close(); response.Close(); return readCount; } catch (Exception ex) { outputStream.Close(); //MessageBox.Show(ex.Message); LogAPI.WriteLog(ex.Message); return -1; } } /// /// Method to upload the specified file to the specified FTP Server /// /// file full name to be uploaded private bool Upload(string filePath, string filename) { FileInfo fileInf = new FileInfo(filename); string uri = "ftp://" + strFTPIP + "/" + filePath + "/" + fileInf.Name; FtpWebRequest reqFTP; // Create FtpWebRequest object from the Uri provided reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strFTPIP + "/" + filePath + "/" + fileInf.Name)); // Provide the WebPermission Credintials reqFTP.Credentials = new NetworkCredential(strFTPUser, strFTPPSW); // 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 using (FileStream fs = fileInf.OpenRead()) { try { // Stream to which the file to be upload is written using (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(); } } catch (Exception ex) { //fs.Close(); LogAPI.WriteLog("Error in getting the Upload!" + ex.Message); return false; } } return true; }