.NetCore使用Renci.SshNet连接SSH将文件传输上去远程Linux服务器

        //linux服务器ip
        private static string host = Appsettings.app(new string[] { "FTPConfig", "FtpIp" });
        //linux服务器登录名
        private static string username = Appsettings.app(new string[] { "FTPConfig", "UserName" });
        //linux服务器密码
        private static string password = Appsettings.app(new string[] { "FTPConfig", "Password" });
         //linux服务器文件存放路径
        private static string UploadPath = Appsettings.app(new string[] { "FTPConfig", "UploadPath" });
        //linux服务器端口
        private static int Port = Appsettings.app(new string[] { "FTPConfig", "Port" }).CastToInt32(); 
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="formFile">文件流</param>
        /// <param name="Path">分类文件名称</param>
        /// <returns></returns>
        public static ReultMessage Upload(IFormFile formFile, string Path)
        {
            Stream stream = null;
            try
            {
                var Extension = Path.GetExtension(formFile.FileName).ToLower();
                if (Extension == ".docx" || Extension == ".pdf")
                {
                    var fileName = $"{formFile.FileName.Replace(Extension, "")}{DateTime.Now.ToString("yyyyMMddhhmmss")}{Extension}";
                    var Paths = UploadPath + Path+ "/" + DateTime.Now.ToString("yyyyMM") + "/";
                    Paths = Path.Combine(Paths);
                    stream = formFile.OpenReadStream();
                    var connectionInfo = new Renci.SshNet.ConnectionInfo(host, Port, username, new PasswordAuthenticationMethod(username, password));
                    using (var sftp = new SftpClient(connectionInfo))
                    {
                        sftp.Connect();
                        if (!sftp.Exists(Paths))
                        {
                            sftp.CreateDirectory(Paths);
                        }
                        sftp.ChangeDirectory(Paths);
                        sftp.UploadFile(stream, fileName, true);
                        sftp.Disconnect();
                        sftp.Dispose();
                    }
                    return new ReultMessage
                    {
                        code = 0,
                        message = "上传成功",
                        data = (Paths + fileName)
                    };
                }
                else
                {
                    return new ReultMessage
                    {
                        code = 1,
                        message = "文件格式不正确"
                    };
                }
            }
            catch (Exception ex)
            {
                return new ReultMessage
                {
                    code = 2,
                    message = ex.Message.ToString()
                };
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }

注意:1、需要NuGet包SSH.NET 2、Linux服务器要安装SSH

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶知秋~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值