1.把本地指定文件上传到FTP文件服务器
private string ftpServer; //FTP的服务器地址
private string uNAME; //FTP服务器的用户名
private string pwd; //FTP服务器的密码
private string ip; //FTP服务器IP
private string port; //FTP服务器端口
/// <summary>
/// FTP文件上传
/// </summary>
/// <param name="ftpPath">要上传到FTP那个目录 /MES系统一课备份/2023年/7月/晶创-哥瑞利MES系统</param>
/// <param name="fileName">要文件名称 20230717_PDAService.txt</param>
/// <param name="localPath">本地文件路径+文件名称 E:\Logs\PDAService\PDAService\20230717_PDAService.txt</param>
/// <param name="timeOut">上传失效时间</param>
/// <returns></returns>
public Boolean FtpUpload(string ftpPath,string fileName, string localPath, int timeOut)
{
//检查目录是否存在,不存在创建
string[] paths = ftpPath.Split("/".ToCharArray());
string prePath = "";
for (int i = 0; i < paths.Length - 1; i++)
{
if (!string.IsNullOrEmpty(paths[i]))
{
prePath += paths[i] + "/";
FtpCheckDirectoryExist(prePath, uNAME, pwd);
}
}
FileInfo fi = new FileInfo(localPath);
var uri = ftpServer + ftpPath+"/"+fileName;//要上传的文件路径,含要上传名称 注:此处必须要加入文件名
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri(uri));
req.Credentials = new NetworkCredential(uNAME, pwd);
req.KeepAlive = true;
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UsePassive = false;
req.UseBinary = true;
req.ContentLength = fi.Length;
req.Timeout = timeOut;
req.ServicePoint.Expect100Continue = false;
FileStream fs = fi.OpenRead();
try
{
Stream stream = req.GetRequestStream();
int BufferLength = 2048; //2K
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
stream.Close();
stream.Dispose();
}
catch
{
return false;
}
finally
{
fs.Close();
req.Abort();
}
return true;
}
//判断文件的目录是否存,不存则创建
private static void FtpCheckDirectoryExist(string destFilePath, string ftpUserID, string ftpPassword)
{
string fullDir = FtpParseDirectory(destFilePath);
try
{
if (fullDir != "")
{
FtpMakeDir(fullDir, ftpUserID, ftpPassword);
}
}
catch (Exception)
{ }
}
private static string FtpParseDirectory(string destFilePath)
{
if (destFilePath.LastIndexOf("/") > 0)
{
return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
}
if (destFilePath.LastIndexOf(@"\") > 0)
{
return destFilePath.Substring(0, destFilePath.LastIndexOf(@"\"));
}
return "";
}
//创建目录
public static Boolean FtpMakeDir(string localFile, string ftpUserID, string ftpPassword)
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(localFile);
req.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
req.Method = WebRequestMethods.Ftp.MakeDirectory;
try
{
FtpWebResponse response = (FtpWebResponse)req.GetResponse();
response.Close();
}
catch (Exception)
{
req.Abort();
return false;
}
req.Abort();
return true;
}
2.从FTP服务器下载文件到本地指定文件夹
/// <summary>
/// FTP下载
/// </summary>
/// <param name="ftpPath">FTP下载路径(不包含FTP的服务器地址)</param>
/// <param name="folderPath">本地路径(下载文件存放路径)</param>
/// <param name="fileName">下载文件名</param>
/// <param name="timeOut"></param>
public void FtpDownload(string ftpPath, string folderPath, string fileName, int timeOut)
{
FtpWebRequest reqFTP;
FileStream outputStream = new FileStream(folderPath + fileName, FileMode.Create);
var uri = ftpServer + ftpPath + "/" + fileName;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.KeepAlive = true;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(uNAME, pwd);
reqFTP.UsePassive = false;
reqFTP.Timeout = timeOut;
FtpWebResponse response = null;
try
{
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();
}
catch (Exception ex)
{
throw ex;
}
finally
{
outputStream.Close();
if (response != null)
{
response.Close();
}
}
}
3.删除FTP服务器指定的文件
/// <summary>
/// 删除FTP指定文件信息
/// </summary>
/// <param name="filePath">FTP服务目录</param>
/// <param name="fileName">删除的文件名称</param>
public void DeleteFile(string filePath,string fileName)
{
try
{
FtpWebRequest reqFTP;
var uri = ftpServer + filePath + "/" + fileName;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(uNAME, pwd);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
}
catch (Exception ex)
{
throw ex;
}
}