c#使用FluentFTP来做FTP的上传下载

最近由于项目需要,做了个FTP的上传下载

上传

/// <summary>
/// 上传共享文件
/// </summary>
/// <param name="server">服务器信息</param>
/// <param name="files">文件列表</param>
public static void UploadFtpFiles(FileServerConfig server, List<TransferFile> files)
{
    Exception ex = null;
    var ftpHost = new Uri(server.UploadServer).Host;
    var ftpPort = new Uri(server.UploadServer).Port == -1 ? 21 : new Uri(server.UploadServer).Port; // 默认FTP端口是21
    var wc = new FtpClient(ftpHost, server.FtpUser, server.FtpPassword, ftpPort);
    wc.Config.DataConnectionType= FtpDataConnectionType.AutoPassive;
    wc.Connect();
    if (files.Count == 0)
    {
        throw new Exception("没有上传文件!");
    }
    
    foreach (var file in files)
    {
        //warn: 不要修改TransferFile对象
        var realTargetFile = server.UploadServer + file.TargetFile;
        if (!Directory.Exists(Path.GetDirectoryName(realTargetFile)))
            Directory.CreateDirectory(Path.GetDirectoryName(realTargetFile));

        //File.Exists无法判断ftp文件是否存在,可以判断share文件是否存在
        if (File.Exists(realTargetFile))
        {
            var backupFile = Path.Combine(Path.GetDirectoryName(realTargetFile), Path.GetFileNameWithoutExtension(realTargetFile) + "_" + CommonHelper.GetDateTimeNowString() + ".bak");
            File.Move(realTargetFile, backupFile);
        }
        Uri uri = new Uri(realTargetFile);
        int count = 3;
        #region 上传出错,执行三次
        while (true)
        {
            var remote = uri.AbsolutePath.Replace("/Attachment", "").Replace("//", "/");
            if (UploadSharedFile(wc, file.SourceFile, remote, count, ref ex))
            {
                wc.Disconnect();
                break;
            }
            count--;
            if (count == 0)
            {
                break;
            }
            Thread.Sleep(1000);
            wc.Dispose();
            wc = new FtpClient();
        }
        #endregion
        if (ex != null)
        {
            break;
        }
    }
    wc.Dispose();
    if (ex != null)
    {
        throw ex;
    }
}
private static bool UploadSharedFile(FtpClient wc, string localFilePath, string remoteFilePath, int count, ref Exception ex)
{
    try
    {
        wc.UploadFile(localFilePath, remoteFilePath, FtpRemoteExists.Overwrite, true);
    }
    catch (Exception e)
    {
        if (count == 1)
        {
            ex = e;
        }
        return false;
    }
    return true;
}

 

 

下载

/// <summary>
/// 下载共享文件
/// </summary>
/// <param name="filePath">下载地址</param>
/// <param name="destinationPath">目标地址</param>
/// <param name="ftpUser">ftp用户名</param>
/// <param name="ftpPassword">ftp密码</param>
public static void DownloadFile(string filePath, string destinationPath, string ftpUser, string ftpPassword)
{
    var fileName = Path.GetFileName(filePath);
    // FTP服务器的地址和端口
    string ftpServerHost = new Uri(filePath).Host;
    int ftpPort = new Uri(filePath).Port == -1 ? 21 : new Uri(filePath).Port; // 默认FTP端口是21
    // 创建FtpClient实例
    using (var client = new FtpClient(ftpServerHost, ftpUser, ftpPassword, ftpPort))
    {
        // Ftp被动模式                                                                
        client.Config.DataConnectionType = FtpDataConnectionType.AutoPassive;
        // 连接到FTP服务器
        client.Connect();
        var localFile = Path.Combine(destinationPath, fileName);
        Uri uri = new Uri(filePath);
        // 下载文件
        client.DownloadFile(localFile, uri.AbsolutePath);
        // 断开连接
        client.Disconnect();
    }
}

 

posted @ 2024-10-29 22:05  韩梦芫  阅读(28)  评论(0编辑  收藏  举报