C# FTP文件上传下载

2021-01-25 09:17

阅读:558

标签:下载失败   syn   cat   cell   端口   null   cancel   load   space   

1.服务端采用Serv-U FTP Server作为FTP服务器端软件。

2.客户端采用开源库FluentFTP

 

 技术图片

 

 

客户端代码

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);
            }

        }
        /// 
        /// 文件上传  返回true 上传成功 false 上传失败
        /// 服务器文件若存在自动覆盖
        /// 
        /// 本地文件地址
        /// 远程文件地址
        /// 触发事件
        /// 
        public static bool UploadFile(string localPath, string remotePath, Actionstring, double> progressAction)
        {
            try
            {
                if (!ftpclient.IsConnected)
                {
                    progressAction?.Invoke(localPath, 500);
                    ftpclient.Dispose();
                    return false;
                }
                Progressdouble> progress = new Progressdouble>(value => {
                    if (value == 100)
                    {
                        progressAction?.Invoke(localPath, value);
                    }
                });
                if (!File.Exists(localPath)) return false;
                bool isSuccess = ftpclient.UploadFile(localPath, remotePath, FtpExists.Overwrite, true, FtpVerify.Retry, progress);
                return isSuccess;
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog(ex);
                return false;
            }
        }
        public static int UploadFiles(IEnumerablestring> localPaths, string remoteDir)
        {
            int successNum = 0;
            try
            {
                successNum = ftpclient.UploadFiles(localPaths, remoteDir, FtpExists.Overwrite, true, FtpVerify.Retry);
                return successNum;
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog(ex);
                return successNum;
            }
        }
        /// 
        /// 异步上传文件
        /// 
        ///  本地文件路径列表
        ///  远程文件目录
        /// task 设置上传文件夹取消
        /// 
        public static Taskint> UploadFilesAsync(IEnumerablestring> localPaths, string remoteDir, CancellationToken token)
        {
            try
            {
                return ftpclient.UploadFilesAsync(localPaths, remoteDir, FtpExists.Overwrite, true, FtpVerify.Retry, FtpError.None, token);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public static void DisConnect()
        {


        }
    }
}
            

 

 

 

 

 

C# FTP文件上传下载

标签:下载失败   syn   cat   cell   端口   null   cancel   load   space   

原文地址:https://www.cnblogs.com/qlbky/p/12016366.html

上一篇:AWT和Swing的简记

下一篇:线程创建方式


评论


亲,登录后才可以留言!