FluentFTP对FtpHelper操作类封装

37 篇文章 0 订阅

github:https://github.com/robinrodricks/FluentFTP

using FluentFTP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace imgmig
{
    public class FtpFileMetadata
    {
        public long FileLength { get; set; }
        public string MD5Hash { get; set; }
        public DateTime LastModifyTime { get; set; }
    }

    public class FtpHelper
    {
        private FtpClient _client = null;
        private string _host = "127.0.0.1";
        private int _port = 21;
        private string _username = "Anonymous";
        private string _password = "";
        private string _workingDirectory = "";
        public string WorkingDirectory
        {
            get
            {
                return _workingDirectory;
            }
        }
        public FtpHelper(string host, int port, string username, string password)
        {
            _host = host;
            _port = port;
            _username = username;
            _password = password;
        }

        public Stream GetStream(string remotePath)
        {
            Open();
            return _client.OpenRead(remotePath);
        }

        public void Get(string localPath, string remotePath)
        {
            Open();
            _client.DownloadFile(localPath, remotePath, true);
        }

        public void Upload(Stream s, string remotePath)
        {
            Open();
            _client.Upload(s, remotePath, FtpExists.Overwrite, true);
        }

        public void Upload(string localFile, string remotePath)
        {
            Open();
            using (FileStream fileStream = new FileStream(localFile, FileMode.Open))
            {
                _client.Upload(fileStream, remotePath, FtpExists.Overwrite, true);
            }
        }

        public int UploadFiles(IEnumerable<string> localFiles, string remoteDir)
        {
            Open();
            List<FileInfo> files = new List<FileInfo>();
            foreach (var lf in localFiles)
            {
                files.Add(new FileInfo(lf));
            }
            int count = _client.UploadFiles(files, remoteDir, FtpExists.Overwrite, true, FtpVerify.Retry);
            return count;
        }

        public void MkDir(string dirName)
        {
            Open();
            _client.CreateDirectory(dirName);
        }

        public bool FileExists(string remotePath)
        {
            Open();
            return _client.FileExists(remotePath);
        }
        public bool DirExists(string remoteDir)
        {
            Open();
            return _client.DirectoryExists(remoteDir);
        }

        public FtpListItem[] List(string remoteDir)
        {
            Open();
            var f = _client.GetListing();
            FtpListItem[] listItems = _client.GetListing(remoteDir);
            return listItems;
        }

        public FtpFileMetadata Metadata(string remotePath)
        {
            Open();
            long size = _client.GetFileSize(remotePath);
            DateTime lastModifyTime = _client.GetModifiedTime(remotePath);

            return new FtpFileMetadata()
            {
                FileLength = size,
                LastModifyTime = lastModifyTime
            };
        }

        public bool TestConnection()
        {
            return _client.IsConnected;
        }

        public void SetWorkingDirectory(string remoteBaseDir)
        {
            Open();
            if (!DirExists(remoteBaseDir))
                MkDir(remoteBaseDir);
            _client.SetWorkingDirectory(remoteBaseDir);
            _workingDirectory = remoteBaseDir;
        }
        private void Open()
        {
            if (_client == null)
            {
                _client = new FtpClient(_host, new System.Net.NetworkCredential(_username, _password));
                _client.Port = 21;
                _client.RetryAttempts = 3;
                if (!string.IsNullOrWhiteSpace(_workingDirectory))
                {
                    _client.SetWorkingDirectory(_workingDirectory);
                }
            }
        }
    }
}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于C#的FTP上传帮助示例: ```csharp using System; using System.IO; using System.Net; public class FtpHelper { private string ftpServerIP; private string ftpUserID; private string ftpPassword; private FtpWebRequest reqFTP; public FtpHelper(string ftpServerIP, string ftpUserID, string ftpPassword) { this.ftpServerIP = ftpServerIP; this.ftpUserID = ftpUserID; this.ftpPassword = ftpPassword; } // 上传文件 public bool Upload(string localFilePath, string remoteFileName) { FileInfo fileInfo = new FileInfo(localFilePath); string uri = "ftp://" + ftpServerIP + "/" + remoteFileName; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.ContentLength = fileInfo.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; using (FileStream fs = fileInfo.OpenRead()) { using (Stream strm = reqFTP.GetRequestStream()) { contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } } } FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); return true; } } ``` 使用方法: ```csharp string localFilePath = @"C:\test.txt"; string remoteFileName = "test.txt"; string ftpServerIP = "ftp://192.168.1.1"; string ftpUserID = "username"; string ftpPassword = "password"; FtpHelper ftpHelper = new FtpHelper(ftpServerIP, ftpUserID, ftpPassword); ftpHelper.Upload(localFilePath, remoteFileName); ``` 其中,`localFilePath`为本地文件路径,`remoteFileName`为远程文件名,`ftpServerIP`为FTP服务器地址,`ftpUserID`和`ftpPassword`为FTP登录凭据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值