C# FTP文件上传下载
2021-01-25 09:17
                         标签:下载失败   syn   cat   cell   端口   null   cancel   load   space    1.服务端采用Serv-U FTP Server作为FTP服务器端软件。 2.客户端采用开源库FluentFTP         客户端代码           C# FTP文件上传下载 标签:下载失败   syn   cat   cell   端口   null   cancel   load   space    原文地址:https://www.cnblogs.com/qlbky/p/12016366.html
using FluentFTP;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
namespace Demo.FTPDownLoad
{
    public static class FTPDownLoadFileHelper
    {
        private static string FtpClientHost = "";//服务端IP
        private static string FtpClientPort = "";//端口号
        private static FluentFTP.FtpClient ftpclient;
        static FTPDownLoadFileHelper()
        {
        
        }
        public static bool FTPDownLoadConnect(string FtpClientHost, int FtpClientPort, string userName, string passKey)
        {
            try
            {
                if (ftpclient != null && ftpclient.IsConnected == true)
                {
                    
                    return true;
                }
                ftpclient = new FluentFTP.FtpClient(FtpClientHost, FtpClientPort, userName, passKey);
                ftpclient.Encoding = Encoding.Default;//设置编码方式
                ftpclient.Connect();//建立连接
                return true;
            }
            catch (Exception ex)
            {
   
                return false;
            }
        }
 
        /// 本地保存地址
        /// 远程文件地址
        /// 下载进度回调
        public static void DownFile(string loaclSaveFilePath, string ftpPath, Actionstring, double> ProgressAction)
        {
            try
            {
                if (!ftpclient.IsConnected)
                {
                    ProgressAction?.Invoke(ftpPath, 500);//服务断开连接,触发下载失败回调
                    ftpclient.Dispose();
                    return;
                }
          //单个文件下载完成触发回调
                Progressdouble> progress = new Progressdouble>(value => {
                    if (value == 100 || value 0)
                    {
                            ProgressAction?.Invoke(ftpPath, value);
                    }
                });
                if (ftpclient.FileExists(ftpPath))
                {
                    ftpclient.DownloadFile(loaclSaveFilePath, ftpPath, FtpLocalExists.Overwrite, FtpVerify.None, progress);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog(ex);
            }
        }
        /// 
上一篇:AWT和Swing的简记
下一篇:线程创建方式