FTPS服务器的上传文件、下载文件、删除指定文件和获取所有文件名

1 篇文章 0 订阅
1 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AlexPilotti.FTPS.Client;
using System.Net;
using AlexPilotti.FTPS.Common;

namespace FTPS
{
    public class FtpsHelper
    {
        /// <summary>
        /// 获取连接对象
        /// </summary>
        /// <param name="FtpIP">IP地址</param>
        /// <param name="Name">登陆用户</param>
        /// <param name="Pass">用户密码</param>
        /// <param name="Msg">错误信息</param>
        /// <param name="Port">端口号(默认21)</param>
        /// <returns>连接对象</returns>
        private static FTPSClient GetClient(string FtpIP, string Name, string Pass, out string Msg, int Port = 21)
        {
            FTPSClient client = new FTPSClient();
            Msg = "";
            try
            {
                NetworkCredential netCredential = new NetworkCredential(Name, Pass);
                client.Connect(
                    FtpIP,
                    Port,
                    netCredential,
                    ESSLSupportMode.All,
                    new System.Net.Security.RemoteCertificateValidationCallback(delegate
                    {
                        return true;
                    }), null, 0, 0, 0, null);
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
            }
            return client;
        }
        /// <summary>
        /// 获取目标Ftps服务器中的文件集合(包括子文件夹)
        /// </summary>
        /// <param name="FtpIP">IP地址</param>
        /// <param name="Name">登陆用户</param>
        /// <param name="Pass">用户密码</param>
        /// <param name="Msg">错误信息</param>
        /// <param name="Port">端口号(默认21)</param>
        /// <returns></returns>
        public static List<DirectoryListItem> GetDirectoryList(string FtpIP, string Name, string Pass, out string Msg, int Port = 21)
        {
            var list = new List<DirectoryListItem>();

            FTPSClient client = GetClient(FtpIP, Name, Pass, out Msg, Port);
            if (Msg != "") { return list; }
            try
            {
                string remoteCurrentPath = client.GetCurrentDirectory();
                list = client.GetDirectoryList(remoteCurrentPath).ToList();
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
            }
            finally
            {
                client.Close();
            }
            return list;
        }
        /// <summary>
        /// 获取目标Ftps服务器中的文件名集合(不包括子文件夹)
        /// </summary>
        /// <param name="FtpIP">IP地址</param>
        /// <param name="Name">登陆用户</param>
        /// <param name="Pass">用户密码</param>
        /// <param name="Msg">错误信息</param>
        /// <param name="Port">端口号(默认21)</param>
        /// <returns></returns>
        public static List<string> GetFiles(string FtpIP, string Name, string Pass, out string Msg, int Port = 21)
        {
            var list = new List<string>();

            FTPSClient client = GetClient(FtpIP, Name, Pass, out Msg, Port);
            if (Msg != "") { return list; }
            try
            {
                string remoteCurrentPath = client.GetCurrentDirectory();
                list = client.GetShortDirectoryList(remoteCurrentPath).ToList();
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
            }
            finally
            {
                client.Close();
            }
            return list;
        }
        /// <summary>
        /// 上传文件到Ftps服务器
        /// </summary>
        /// <param name="FtpIP">IP地址</param>
        /// <param name="Name">登陆用户</param>
        /// <param name="Pass">用户密码</param>
        /// <param name="FilePath">要上传的文件路径(包括文件名)</param>
        /// <param name="Msg">错误信息</param>
        /// <param name="SaveName">保存到目标Ftps服务器中的文件名(默认为空,则为原文件名)</param>
        /// <param name="Port">端口号(默认21)</param>
        /// <returns>是否成功</returns>
        public static bool UploadFile(string FtpIP, string Name, string Pass, string FilePath, out string Msg, string SaveName = "", int Port = 21)
        {
            bool result = false;

            FTPSClient client = GetClient(FtpIP, Name, Pass, out Msg, Port);
            if (Msg != "") { return result; }
            try
            {
                if (SaveName == "")
                {
                    int index = FilePath.LastIndexOf('\\');
                    SaveName = FilePath.Substring(index + 1);
                }
                client.PutFile(FilePath, "/" + SaveName);
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
                Msg = ex.Message;
            }
            finally
            {
                client.Close();
            }
            return result;
        }
        /// <summary>
        /// 下载文件到Ftps服务器
        /// </summary>
        /// <param name="FtpIP">IP地址</param>
        /// <param name="Name">登陆用户</param>
        /// <param name="Pass">用户密码</param>
        /// <param name="FileName">要下载的文件名</param>
        /// <param name="LocaPath">本地存放地址(不包括文件名)</param>
        /// <param name="Msg">错误信息</param>
        /// <param name="SaveName">保存到本地存放地址中的文件名(默认为空,则为原文件名)</param>
        /// <param name="Port">端口号(默认21)</param>
        /// <returns>是否成功</returns>
        public static bool DownloadFile(string FtpIP, string Name, string Pass, string FileName, string LocaPath, out string Msg, string SaveName = "", int Port = 21)
        {
            bool result = false;

            FTPSClient client = GetClient(FtpIP, Name, Pass, out Msg, Port);
            if (Msg != "") { return result; }
            try
            {
                if (SaveName == "")
                {
                    SaveName = FileName;
                }
                client.GetFile("/" + FileName, LocaPath + "\\" + SaveName);
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
                Msg = ex.Message;
            }
            finally
            {
                client.Close();
            }
            return result;
        }
        /// <summary>
        /// 删除Ftps服务器中的文件
        /// </summary>
        /// <param name="FtpIP">IP地址</param>
        /// <param name="Name">登陆用户</param>
        /// <param name="Pass">用户密码</param>
        /// <param name="FileName">要删除的文件名</param>
        /// <param name="Msg">错误信息</param>
        /// <param name="Port">端口号(默认21)</param>
        /// <returns>是否成功</returns>
        public static bool DeleteFile(string FtpIP, string Name, string Pass, string FileName, out string Msg, int Port = 21)
        {
            bool result = false;

            FTPSClient client = GetClient(FtpIP, Name, Pass, out Msg, Port);
            if (Msg != "") { return result; }
            try
            {
                client.DeleteFile(FileName);
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
                Msg = ex.Message;
            }
            finally
            {
                client.Close();
            }
            return result;
        }
    }
}

然后,里面具体引用的类文件,我下文件的外网官网找不到了;

不过,可以在https://github.com/StingyJack/FTPSClient(不保证一定和我用的版本相同)下载,

另外,也可以下载我本人上传的资源文件,地址:https://download.csdn.net/download/caojunzhi96/11421384

前天布置的好好的,今天突然发现又不起作用了,发现报错:流不可读

实际原因是因为: "由于远程方关闭传输流,身份验证失败",后面发现应该是FTPS服务器的SSL版本发生变化,由原来的TLS变为TLS11

在FTPSClient类文件里做如下修改即可:

(注:如果引用的是我上传的,可以直接在相同行数直接套用)

在此,特别感谢:https://www.jianshu.com/p/802de8843e21

https://www.cnblogs.com/eastday/p/6043631.html

让我有思路和解决方法

微软官方的介绍页面

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值