SFTP对接Linux

//需要引用Renci.SshNet.dll 

 

    /// <summary>
    /// SFTP操作类
    /// </summary>
    public class SFTPHelper
    {
        #region 字段或属性

        private string pwd = string.Empty;

        private SftpClient sftp;
        /// <summary>
        /// SFTP连接状态
        /// </summary>
        public bool Connected { get { return sftp.IsConnected; } }
        #endregion

        #region 构造

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="SFtpConfig"></param>
        public SFTPHelper(SFTPConfig SFtpConfig)
        {
            //处理KeyBoard验证的解决方案
            KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(SFtpConfig.username);
            PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(SFtpConfig.username, SFtpConfig.pwd);
            kauth.AuthenticationPrompt += new EventHandler<Renci.SshNet.Common.AuthenticationPromptEventArgs>(HandleKeyEvent);
            ConnectionInfo connectionInfo = new ConnectionInfo(SFtpConfig.Server, SFtpConfig.port, SFtpConfig.username, pauth, kauth);
            this.sftp = new SftpClient(connectionInfo);
            this.pwd = SFtpConfig.pwd;
        }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="ip">IP</param>
        /// <param name="port">端口</param>
        /// <param name="user">用户名</param>
        /// <param name="pwd">密码</param>
        public SFTPHelper(string server, int port, string user, string pwd)
        {
            //处理KeyBoard验证的解决方案
            KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(user);
            PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(user, pwd);
            kauth.AuthenticationPrompt += new EventHandler<Renci.SshNet.Common.AuthenticationPromptEventArgs>(HandleKeyEvent);
            ConnectionInfo connectionInfo = new ConnectionInfo(server, port, user, pauth, kauth);
            this.sftp = new SftpClient(connectionInfo);
            this.pwd = pwd;
        }

        #endregion

        //处理KeyBoard验证的解决方案
        void HandleKeyEvent(Object sender, Renci.SshNet.Common.AuthenticationPromptEventArgs e)
        {
            foreach (Renci.SshNet.Common.AuthenticationPrompt prompt in e.Prompts)
                if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1)
                    prompt.Response = this.pwd;
        }
        
        #region 连接SFTP
        /// <summary>
        /// 连接SFTP
        /// </summary>
        /// <returns>true成功</returns>
        public bool Connect()
        {
            try
            {
                if (!Connected)
                {
                    sftp.Connect();
                }
                return true;
            }
            catch (Exception ex)
            {
                //return false;
                // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("连接SFTP失败,原因:{0}", ex.Message));
                throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
                //连接SFTP失败,原因:An established connection was aborted by the software in your host machine.
            }
        }
        #endregion

        #region 断开SFTP
        /// <summary>
        /// 断开SFTP
        /// </summary> 
        public void Disconnect()
        {
            try
            {
                if (sftp != null && Connected)
                {
                    sftp.Disconnect();
                }
            }
            catch (Exception ex)
            {
                // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("断开SFTP失败,原因:{0}", ex.Message));
                throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));
            }
        }
        #endregion

        #region SFTP上传文件
        /// <summary>
        /// SFTP上传文件
        /// </summary>
        /// <param name="localPath">本地路径</param>
        /// <param name="remotePath">远程路径</param>
        public void Put(string localPath, string remotePath)
        {
            try
            {
                using (var file = File.OpenRead(localPath))
                {
                    Connect();
                    sftp.UploadFile(file, remotePath);
                    Disconnect();
                }
            }
            catch (Exception ex)
            {
                // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
                throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
            }
        }
        #endregion

        #region SFTP获取文件
        /// <summary>
        /// SFTP获取文件
        /// </summary>
        /// <param name="remotePath">远程路径</param>
        /// <param name="localPath">本地路径</param>
        public void Get(string remotePath, string localPath)
        {
            try
            {
                Connect();
                var byt = sftp.ReadAllBytes(remotePath);
                Disconnect();
                File.WriteAllBytes(localPath, byt);
            }
            catch (Exception ex)
            {
                // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
                throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
            }

        }
        #endregion

        #region 删除SFTP文件
        /// <summary>
        /// 删除SFTP文件 
        /// </summary>
        /// <param name="remoteFile">远程路径</param>
        public void Delete(string remoteFile)
        {
            try
            {
                Connect();
                sftp.Delete(remoteFile);
                Disconnect();
            }
            catch (Exception ex)
            {
                // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
                throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
            }
        }
        #endregion

        private int day(string name)
        {
            if (!name.Contains("_"))
                return 0;
            name = name.Split('_')[1];
            if (name.Length < 8)
                return 0;
            name = name.Substring(0, 8);
            DateTime start = DateTime.ParseExact(name, "yyyyMMdd", null);
            DateTime end = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            return sp.Days;
        }

        #region 获取SFTP文件列表
        /// <summary>
        /// 获取SFTP文件列表
        /// </summary>
        /// <param name="remotePath">远程目录</param>
        /// <param name="fileSuffix">文件后缀</param>
        /// <returns></returns>
        public ArrayList GetFileList(string remotePath, string fileSuffix)
        {
            try
            {
                Connect();
                var files = sftp.ListDirectory(remotePath);
                Disconnect();
                var objList = new ArrayList();
                foreach (var file in files)
                {
                    /*DateTime start = Convert.ToDateTime(file.LastWriteTime.ToShortDateString());

                    DateTime end = Convert.ToDateTime(DateTime.Now.ToShortDateString());
                    TimeSpan sp = end.Subtract(start);*/
                    string name = file.Name;
                    if (day(name) != 1)
                        continue;
                    if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))
                    {
                        objList.Add(name);
                    }
                }
                return objList;
            }
            catch (Exception ex)
            {
                // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
                throw new Exception(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
            }
        }
        #endregion

        #region 移动SFTP文件
        /// <summary>
        /// 移动SFTP文件
        /// </summary>
        /// <param name="oldRemotePath">旧远程路径</param>
        /// <param name="newRemotePath">新远程路径</param>
        public void Move(string oldRemotePath, string newRemotePath)
        {
            try
            {
                Connect();
                sftp.RenameFile(oldRemotePath, newRemotePath);
                Disconnect();
            }
            catch (Exception ex)
            {
                // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
                throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
            }
        }
        #endregion

        public static int DownloadFtp(string filePath, string localPath, string fileName, string ftpServerIP, string ftpPort, string ftpUserID, string ftpPassword)
        {
            string localFileName = localPath + "/" + fileName;
            string remoteFileName = filePath + "/" + fileName;

            try
            {
                using (var sftp = new SftpClient(ftpServerIP, Convert.ToInt32(ftpPort), ftpUserID, ftpPassword))
                {
                    sftp.Connect();

                    using (var file = File.OpenWrite(localFileName))
                    {
                        sftp.DownloadFile(remoteFileName, file);
                    }

                    sftp.Disconnect();
                    //Log.getInstace().WriteSysInfo("下载文件{localFileName}成功", "info");  

                    Console.WriteLine("下载文件成功,文件路径:{localFileName}");
                    return 0;
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", e.Message));
                return -2;
            }
        }

    }

    /// <summary>
    /// SFTP配置信息
    /// </summary>
    public class SFTPConfig
    {
        private string _server = string.Empty;
        public string Server { get { return _server; } set { _server = value; } }

        private int _port = 80;
        public int port { get { return _port; } set { _port = value; } }

        private string _username = string.Empty;
        public string username { get { return _username; } set { _username = value; } }

        private string _pwd = string.Empty;
        public string pwd { get { return _pwd; } set { _pwd = value; } }
    }

 

/*****************************************
   调用上传SFTP数据 
   SFTPConfigString为账号等配置信息
   LocalCsvFilePath本地要上传的文件地址
   SFTPUpLoadCsvFilePath为SFTP虚拟目录
******************************************/

SFTPConfig SFTPConfig = JsonConvert.DeserializeObject<SFTPConfig>(SFTPConfigString);
SFTPHelper SFTPHelper = new SFTPHelper(SFTPConfig);
SFTPHelper.Put(LocalCsvFilePath, SFTPUpLoadCsvFilePath);

 

posted @ 2020-07-20 14:25  全栈攻城师  阅读(352)  评论(0)    收藏  举报