前面寫了采用ftp上傳文件,有了上傳怎麼能夠沒有下載呢?如果只有上傳沒有下載,那上傳了也沒啥用了。所以今天就跟大家一起學習學習使用ftp下載文件。
知道了怎麼上傳,那麼下載也就變得很簡單了,上傳是把文件放到服務器,而下載是把文件從服務器取過來。一個是從本地讀文件,然後寫到服務器;另一個是從服務器讀文件,然後寫到本地。基本原理就是這樣,下面我們看看具體的代碼:
[csharp]
/// <summary>
/// FTP下載文件
/// </summary>
/// <param name="userId">ftp用戶名</param>
/// <param name="pwd">ftp密碼</param>
/// <param name="ftpPath">ftp文件路徑</param>
/// <param name="filePath">下載保存路徑</param>
/// <param name="fileName">ftp文件名</param>
/// <returns></returns>
public string Download(string userId, string pwd, string ftpPath, string filePath, string fileName)
{
string sRet = "下載成功!";
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
// 根據uri創建FtpWebRequest對象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
// 指定執行什麼命令
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
// 指定數據傳輸類型
reqFTP.UseBinary = true;
reqFTP.UsePassive = false;
// ftp用戶名和密碼
reqFTP.Credentials = new NetworkCredential(userId, pwd);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
// 把下載的文件寫入流
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
// 緩沖大小設置為2kb
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
// 每次讀文件流的2kb
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
// 把內容從文件流寫入
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
//關閉兩個流和ftp連接
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
sRet=ex.Message;
}
//返回下載結果(是否下載成功)
return sRet;
}
/// <summary>
/// FTP下載文件
/// </summary>
/// <param name="userId">ftp用戶名</param>
/// <param name="pwd">ftp密碼</param>
/// <param name="ftpPath">ftp文件路徑</param>
/// <param name="filePath">下載保存路徑</param>
/// <param name="fileName">ftp文件名</param>
/// <returns></returns>
public string Download(string userId, string pwd, string ftpPath, string filePath, string fileName)
{
string sRet = "下載成功!";
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
// 根據uri創建FtpWebRequest對象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
// 指定執行什麼命令
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
// 指定數據傳輸類型
reqFTP.UseBinary = true;
reqFTP.UsePassive = false;
// ftp用戶名和密碼
reqFTP.Credentials = new NetworkCredential(userId, pwd);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
// 把下載的文件寫入流
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
// 緩沖大小設置為2kb
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
// 每次讀文件流的2kb
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
// 把內容從文件流寫入
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
//關閉兩個流和ftp連接
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
sRet=ex.Message;
}
//返回下載結果(是否下載成功)
return sRet;
}
以上代碼可以實現一個簡單的ftp下載的功能,只要在需要的地方調用此方法即可。代碼很簡單,功能很實用。
還有很多FTP的相關操作,以後會陸續跟大家分享,敬請期待!