为了测试我们写的代码是否具有ftp上传下载功能,我们需要去下载一个虚拟FTP服务器,如:Serv-U。配置好Serv-U后,我们就可以编码测试了。
引入命名空间:using System.Net;
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filePath">文件下载后存放路径</param>
/// <param name="SaveFileName">文件下载后的名称</param>
/// <param name="parFileName">要下载的文件名</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// Method to upload the specified file to the specified FTP Server
/// </summary>
/// <param name="filename">file full name to be uploaded</param>
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;
}