FtpWebRequest 实现向FTP服务器上传文件

编程开发
117
C#
FtpWebRequest
FTP传文件

FtpWebRequest 实现向FTP服务器上传文件

最近一个虚拟打印的需求 就是监听文件然后把文件上传到Ftp服务器上 自己c#实现封装了个上传类 用着还不错 这里分享下

   public class FtpUpload
    {
        string _host { get; set; }
        string _port { get; set; }
        string _user { get; set; }
        string _password { get; set; }
        string _relative { get; set; }
        string _usepassive { get; set; }

        private FtpUpload(string host, string port, string user, string password,string relative,string usepassive)
        {
            _host = host;
            _port = port;
            _user = user;
            _password = password;
            _relative = relative;
            _usepassive = usepassive;
        }

        public static FtpUpload Create()
        {
		 //ftp服务器IP
            string UploadServerHost = ConfigurationManager.AppSettings.Get("UploadServerHost");
			//端口
            string UploadServerPort = ConfigurationManager.AppSettings.Get("UploadServerPort");
			//相对目录
            string RelativeDirectory = ConfigurationManager.AppSettings.Get("RelativeDirectory");
			//用户名
            string UserName = ConfigurationManager.AppSettings.Get("UserName");
			//密码
            string UserPassword = ConfigurationManager.AppSettings.Get("UserPassword");
			//被动模式
            string UsePassive=ConfigurationManager.AppSettings.Get("UsePassive");
            return new FtpUpload(UploadServerHost, UploadServerPort, UserName, UserPassword, RelativeDirectory, UsePassive);
        }

        public bool Connected()
        {
            byte[] buf = new byte[1024];
            TcpClient b = new TcpClient();
            int portInt = 0;
            int.TryParse(_port, out portInt);
            b.Connect(_host, portInt); //IP及端口
            return b.Connected;
        }

        /// 
        /// 上传文件
        /// 
        /// 
        /// 
        public bool Upload(string LoctionPath)
        {
            try
            {
                if (!_relative.StartsWith("/"))
                {
                    _relative = $"/{_relative}";
                }

                _relative.Replace("//", "/");
                string RelativePath = $"ftp://{_host}:{_port}/{_relative}/{Guid.NewGuid()}.pdf";

                FtpWebRequest FtpClient = (FtpWebRequest)FtpWebRequest.Create(new Uri(RelativePath));
                FtpClient.Credentials = new NetworkCredential(_user, _password);
                FtpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
                FtpClient.UseBinary = true;
                bool UsePassive = false;
                bool.TryParse(_usepassive, out UsePassive);
                FtpClient.UsePassive = UsePassive;
                FtpClient.KeepAlive = false;

                using (FileStream fs = new FileStream(LoctionPath,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite))
                {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    fs.Close();
                    fs.Dispose();
                    using (Stream ftpstream = FtpClient.GetRequestStream())
                    {
                        ftpstream.Write(buffer, 0, buffer.Length);
                        ftpstream.Close();
                        ftpstream.Dispose();
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                Log4Net.Error($"【Ftp文件上传失败】 {LoctionPath}", ex);
            }
            return false;
        }

    }

调用例子

FtpUpload ftp=FtpUpload.Create();
ftp.Upload("文件地址")
c# 获取本机电脑名称、Ip信息
多线程使用Image.FromFile 加载图片时出现图片文件被锁定无法更新
C#怎么获取windows系统支持的字体以及字体样式
C#解析获取GIF帧图像
C#开发怎么将Bitmap转换成BitmapImage
.NET5框架下使用HttpListener类实现http接口监听替代OWIN自我寄宿
C# 设置文件指定默认打开程序的设置方法
C#使用 CefSharp采集网页源html代码
C# HttpWebRequest POST 简单例子
wpf调用文件目录打开目录选择对话框
PDF文件流接口弹出下载提示不是直接预览的解决办法
c# 实现windows服务程序的安装、启动、暂停以及卸载 等管理功能
暂无相关内容...
C#结合虫洞软件(Wormhole)实现自动登录到手机版百度网盘的流程源码
[人脸检测]基于C#OpenCvSharp+haarcascade人脸、人眼模型实现人脸检测源码
基于.NET4.0实现的目录文件新增监视并自动化上传至ftp完整流程工具源码
C#基于OpenCVSharp实现图片对比相似度百分比源码下载
.NET5+C# WPF实现图片添加倾斜文字平铺水印工具源码
免责声明 部分转载分享内容若侵犯您的权益,还请 邮件联系 侵删