193 lines
5.9 KiB
C#
193 lines
5.9 KiB
C#
using Cowain.Bake.Common.Core;
|
||
using FluentFTP;
|
||
using System;
|
||
using System.IO;
|
||
|
||
namespace Cowain.Bake.Communication.FTP
|
||
{
|
||
//现在是的是短连接,即上传完就关闭; 后期如果有效率上的要求,要改成长连接,就是过程中不关闭连接
|
||
//建立好连接,约:700毫秒
|
||
//关闭连接,约:90毫秒
|
||
//全部都是远程操作
|
||
public class FtpHelper
|
||
{
|
||
#region 属性与构造函数
|
||
public FtpClient ftpClient { get; set; } ///
|
||
public string IpAddr { get; set; } /// IP地址
|
||
public string RelatePath { get; set; } /// 相对路径
|
||
public int Port { get; set; } /// 端口号
|
||
public string UserName { get; set; } /// 用户名
|
||
public string Password { get; set; } /// 密码
|
||
public bool IsConnected { get; set; } = false;
|
||
|
||
//https://blog.csdn.net/fengershishe/article/details/129140618
|
||
|
||
public FtpHelper(string ipAddr, int port, string userName, string password, string relatePath = "")
|
||
{
|
||
this.IpAddr = ipAddr;
|
||
this.Port = port;
|
||
this.UserName = userName;
|
||
this.Password = password;
|
||
//this.RelatePath = relatePath;
|
||
}
|
||
#endregion
|
||
|
||
public bool Connect()
|
||
{
|
||
try
|
||
{
|
||
Close();
|
||
//using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
|
||
ftpClient = new FtpClient(this.IpAddr, this.Port);
|
||
ftpClient.Connect(); // 连接到FTP服务器
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.Error($"Ftp Connect {ex.Message}");
|
||
IsConnected = false;
|
||
return false;
|
||
}
|
||
|
||
IsConnected = ftpClient.IsConnected;
|
||
return IsConnected;
|
||
}
|
||
|
||
bool IsConnect()
|
||
{
|
||
if (null == ftpClient || !ftpClient.IsConnected || !IsConnected)
|
||
{
|
||
Connect();
|
||
}
|
||
|
||
return IsConnected;
|
||
}
|
||
|
||
public void Close()
|
||
{
|
||
if (null != ftpClient)
|
||
{
|
||
ftpClient.Disconnect();
|
||
ftpClient.Dispose();
|
||
ftpClient = null;
|
||
}
|
||
}
|
||
|
||
~FtpHelper()
|
||
{
|
||
Close();
|
||
}
|
||
|
||
#region 方法
|
||
public FtpListItem[] ListDir()
|
||
{
|
||
FtpListItem[] lists = null;
|
||
if (IsConnect())
|
||
{
|
||
ftpClient.SetWorkingDirectory(this.RelatePath);
|
||
lists = ftpClient.GetListing(); //当前路径下,返回所有文件
|
||
}
|
||
return lists;
|
||
}
|
||
|
||
// 上传文件
|
||
public bool UpLoad(string srcFilePath, string descPath)
|
||
{
|
||
string descFilePath = null;
|
||
try
|
||
{
|
||
if (IsConnect())
|
||
{
|
||
string fileName = Path.GetFileName(srcFilePath);
|
||
char lastChar = descPath[descPath.Length - 1];
|
||
|
||
if ('\\' == lastChar
|
||
|| '/' == lastChar)
|
||
{
|
||
descFilePath = descPath + fileName; //descPath, 最后一个,是"/"
|
||
}
|
||
else
|
||
{
|
||
descFilePath = descPath + "/" + fileName; //descPath, 最后一个,是"/"
|
||
}
|
||
|
||
FtpStatus result = ftpClient.UploadFile(srcFilePath, descFilePath); // ("local/file.txt", "remote/file.txt");
|
||
return FtpStatus.Success == result ? true : false;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
IsConnected = false;
|
||
LogHelper.Instance.Error($"Ftp UpLoad {ex.Message}");
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
///创建目录(远程创建)
|
||
private bool CheckDirIsExists(string dir)
|
||
{
|
||
bool flag = false;
|
||
try
|
||
{
|
||
if (IsConnect())
|
||
{
|
||
ftpClient.SetWorkingDirectory(this.RelatePath);
|
||
flag = ftpClient.DirectoryExists(dir);
|
||
if (!flag)
|
||
{
|
||
flag = ftpClient.CreateDirectory(dir); //client.CreateDirectory("remote/directory");
|
||
return flag;
|
||
}
|
||
}
|
||
|
||
return flag;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
IsConnected = false;
|
||
LogHelper.Instance.Error($"Ftp CheckDirIsExists {ex.Message}");
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public bool DeleteFile(string dir)
|
||
{
|
||
try
|
||
{
|
||
if (IsConnect())
|
||
{
|
||
ftpClient.SetWorkingDirectory(this.RelatePath);
|
||
ftpClient.DeleteFile(dir);
|
||
return true;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
IsConnected = false;
|
||
LogHelper.Instance.Error($"Ftp DeleteFile {ex.Message}");
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public bool DownloadFile(string localAddress, string remoteAddress)
|
||
{
|
||
try
|
||
{
|
||
if (IsConnect())
|
||
{
|
||
ftpClient.SetWorkingDirectory(this.RelatePath);
|
||
FtpStatus status = ftpClient.DownloadFile(localAddress, remoteAddress);
|
||
return status == FtpStatus.Success ? true : false;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
IsConnected = false;
|
||
LogHelper.Instance.Error($"Ftp DownloadFile {ex.Message}");
|
||
}
|
||
|
||
return false;
|
||
}
|
||
#endregion
|
||
}
|
||
} |