声明全局ftpwebrequest和ftpwebresponse:
private FtpWebRequest ftpWebRequest = null;
private FtpWebResponse ftpWebResponse = null;
调用方法:
public bool PushImages(string localPath, string dirName, string ftpServerString)
{
bool result = false;
try
{
FtpServer ftpServer = DataSettingsHelper.GetFtpServerSetting(ftpServerString);
string ftpPath = @"Ftp://" + ftpServer.IP + @"/";
ftpServer.UserName = (ftpServer.UserName == null ? "" : ftpServer.UserName);
ftpServer.Password = (ftpServer.Password == null ? "" : ftpServer.Password);
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
ftpWebRequest.Credentials = new NetworkCredential(ftpServer.UserName, ftpServer.Password);
ftpWebRequest.UseBinary = true;
ftpWebRequest.ConnectionGroupName = "FtpConnectionGroup";
ftpWebRequest.KeepAlive = true;
UploadDirectory(localPath, ftpPath + ftpServer.InitDir + @"/", dirName);
result = true;
}
catch (Exception ex)
{
_logger.Error("Pushing Images", ex);
result = false;
}
finally
{
if (ftpWebResponse != null)
{
ftpWebResponse.Close();
}
}
return result;
}
上传文件方法:
private void UpLoadFile(string localFile, string ftpPath)
{
if (!System.IO.File.Exists(localFile))
{
return;
}
FileStream localFileStream = null;
Stream requestStream = null;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
// ftpWebRequest.ConnectionGroupName = "FtpConnectionGroup";
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.ContentLength = localFile.Length;
int buffLength = 4096;
byte[] buff = new byte[buffLength];
int contentLen;
localFileStream = new FileInfo(localFile).OpenRead();
requestStream = ftpWebRequest.GetRequestStream();
contentLen = localFileStream.Read(buff, 0, buffLength);
while (contentLen != 0)
{
requestStream.Write(buff, 0, contentLen);
contentLen = localFileStream.Read(buff, 0, buffLength);
}
if (requestStream != null)
{
requestStream.Close();
}
if (localFileStream != null)
{
localFileStream.Close();
}
}
获取本地目录文件和子目录
private List<List<string>> GetDirDetails(string localDir)
{
List<List<string>> infos = new List<List<string>>();
infos.Add(System.IO.Directory.GetFiles(localDir).ToList());
infos.Add(System.IO.Directory.GetDirectories(localDir).ToList());
for (int i = 0; i < infos[0].Count; i++)
{
int index = infos[0][i].LastIndexOf(@"\");
infos[0][i] = infos[0][i].Substring(index + 1);
}
for (int i = 0; i < infos[1].Count; i++)
{
int index = infos[1][i].LastIndexOf(@"\");
infos[1][i] = infos[1][i].Substring(index + 1);
}
return infos;
}
上传文件和子目录:
private void UploadDirectory(string localDir, string ftpPath, string dirName)
{
string dir = localDir + dirName + @"\";
if (!System.IO.Directory.Exists(dir))
{
return;
}
MakeDir(ftpPath, dirName);
List<List<string>> infos = GetDirDetails(dir);
for (int i = 0; i < infos[0].Count; i++)
{
UpLoadFile(dir + infos[0][i], ftpPath + dirName + @"/" + infos[0][i]);
}
for (int i = 0; i < infos[1].Count; i++)
{
UploadDirectory(dir, ftpPath + dirName + @"/", infos[1][i]);
}
}
创建子目录,存在此目录时,会报错,需要不处理这个错误,没有更好的办法判断ftp服务器上是否存在某目录:
private void MakeDir(string ftpPath, string dirName)
{
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + dirName));
ftpWebRequest.ConnectionGroupName = "FtpConnectionGroup";
ftpWebRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
try
{
ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
}
catch
{
}
}