1、实体类
public class FtpsModel
{
/// <summary>
/// 传输时间
/// </summary>
public string time_ftp { get; set; }
/// <summary>
/// 传输年份
/// </summary>
public string year_ftp { get; set; }
/// <summary>
/// 传输月份
/// </summary>
public string month_ftp { get; set; }
/// <summary>
/// 传输日
/// </summary>
public string day_ftp { get; set; }
/// <summary>
/// 本地文件名
/// </summary>
public string fileName { get; set; }
/// <summary>
/// 本地文件路径
/// </summary>
public string FileDirName { get; set; }
/// <summary>
/// ftsp服务器文件名
/// </summary>
public string NewFileName { get; set; } = "";
/// <summary>
/// ftsp服务器文件路径
/// </summary>
public string NewFilePath { get; set; } = "";
}
2、文件上传
private void btnUpload_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtPort.Text))
{
MessageBox.Show("请填写ftp地址!");
return;
}
if (string.IsNullOrEmpty(txtPort.Text))
{
MessageBox.Show("请填写端口!");
return;
}
if (string.IsNullOrEmpty(txtUser.Text))
{
MessageBox.Show("请填写用户名!");
return;
}
if (string.IsNullOrEmpty(txtPwd.Text))
{
MessageBox.Show("请填写密码!");
return;
}
if (string.IsNullOrEmpty(txtFile.Text))
{
MessageBox.Show("请选择文件!");
return;
}
FtpsModel model = new FtpsModel();
model.time_ftp = DateTime.Now.ToString("yyyyMMddhhmmss");
model.fileName = "";
model.NewFileName="ftp_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileName;
model.year_ftp = DateTime.Now.Year.ToString();
model.month_ftp = DateTime.Now.Month.ToString();
model.day_ftp = DateTime.Now.Day.ToString();
model.FileDirName = txtFile.Text ;
UpLoadFile(model);
}
/// <summary>
/// Ftps上传文件对外方法
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool UpLoadFile(FtpsModel model)
{
//读取本机文件存放目录
var fileDir = model.FileDirName;
//组合传过来的文件相对路径
var filePath_ftp = model.year_ftp + "\\" + model.month_ftp + "\\" + model.day_ftp + "\\";
var localFilePath = Path.Combine(fileDir, model.fileName);
model.NewFilePath = filePath_ftp + model.NewFileName;
model.NewFilePath = model.NewFilePath.Replace("\\", "/");
//写日志
string infoFormat =string.Format("Ftps开始上传文件,本地文件路径:{0},FTP服务端创建的目录路径:{1},文件名:{2}", model.FileDirName, filePath_ftp, model.NewFilePath);
Console.WriteLine(infoFormat);
return Upload(localFilePath, filePath_ftp, model.NewFileName);
}
/// <summary>
/// 执行Ftps上传文件
/// </summary>
/// <param name="filePath">本地文件的绝对路径</param>
/// <param name="filePath_ftp">要在FTP服务端创建目录的相对路径</param>
/// <param name="fileName">文件名</param>
private bool Upload(string filePath, string filePath_ftp, string fileName)
{
try
{
//判断是否存在当前本地文件
if (!File.Exists(filePath))
{
string infoFormat = "Upload - Ftps上传文件失败,找不到本地文件";
MessageBox.Show(infoFormat);
}
using (Ftp client = new Ftp())
{
if (connectType == 0)
{
//ftp上传
client.Connect(ip, Ftp.DefaultSSLPort);
}
else if (connectType == 2)
{
//显式SSL使用与常规FTP(21)相同的端口。
//常规连接后,客户端明确要求服务器保护连接。使用“ AUTH TLS”命令可以做到这一点。
//https://www.limilabs.com/blog/use-ssl-with-ftp-explicit
client.ServerCertificateValidate += ValidateCertificate;
//显示ftps上传
client.Connect(ip, Ftp.DefaultSSLPort);
client.AuthTLS();
}
else if (connectType == 1)
{
client.ServerCertificateValidate += ValidateCertificate;
//隐式ftps上传
client.ConnectSSL(ip);
}
client.Login(user, pwd);
var filestr = filePath_ftp.Replace("\\", "/");
//创建目录
client.CreateAllFolders(filestr);
Thread.Sleep(50);
//打开目录
client.ChangeFolder(filestr);
//为防止第三方组件重命名已存在的文件,判断已存在当前名字的,先删除文件
if (client.FileExists(fileName))
{
client.DeleteFile(fileName);
Thread.Sleep(50);
}
//上传文件
client.Upload(fileName, filePath);
client.Close();
//写日志
string infoFormat =string.Format("Upload -Ftps上传文件成功,本地文件路径:{0},FTP服务端创建的目录路径:{1},文件名:{2}", filePath, filePath_ftp, fileName);
MessageBox.Show(infoFormat);
}
return true;
}
catch (Exception ex)
{
MessageBox.Show("Upload-Ftps上传文件失败:" + ex.Message);
return false;
}
finally
{
}
}
private static void ValidateCertificate(object sender, ServerCertificateValidateEventArgs e)
{
const SslPolicyErrors ignoredErrors =
SslPolicyErrors.RemoteCertificateChainErrors |
SslPolicyErrors.RemoteCertificateNameMismatch;
if ((e.SslPolicyErrors & ignoredErrors) != 0)
{
e.IsValid = true;
return;
}
e.IsValid = false;
}
3、测试结果
(1)ftp上传
(2)显式ftps上传
(3)隐式ftps上传