62,046
社区成员
发帖
与我相关
我的任务
分享
private void ftpDownLoadFile(string serverPath, string clientPath)
{
string FtpServer = teForm.UserInformation.FTPAddress;
string FtpUserName = teForm.UserInformation.FTPUser;
string FtpPassword = teForm.UserInformation.FTPPWD;
string FtpFileAddress = "ftp://" + FtpServer + "/" + serverPath;
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(clientPath, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpFileAddress));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FtpUserName, 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)
{
Response.Write(ex.Message);
}
}