asp.net ftp文件上传类

作者:袖梨 2022-06-25

只实现了基本的上传、下载、判断文件是否存在及我实际应用中最重要的根据选择的远程文件地址自动创建相关目录功能

具体代码下载:FtpHelper

编辑器插入Code总是报错只能直接贴代码了,汗。。。

 using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
namespace System.Helper
{
    ///


    /// 创建者:懒惰的肥兔
    /// FTP操作类
    /// 只支持基本的文件上传、下载、目录递归创建、文件目录列表获取
    ///

    public class FtpHelper
    {
        #region 属性
        ///
        /// 获取或设置用户名
        ///

        public string UserName { get; set; }
        ///
        /// 获取或设置密码
        ///

        public string Password { get; set; }
        ///
        /// 异常信息
        ///

        public string ErrorMsg { get; set; }
        ///
        /// Exception
        ///

        public Exception Exception { get; set; }
        ///
        /// 状态
        ///

        public FtpStatusCode StatusCode { get; set; }
        ///
        /// 状态描述
        ///

        public string StatusDescription { get; set; }
        ///
        /// 获取或设置FTP服务器地址
        ///

        public Uri Uri { get; set; }
        ///
        /// 获取或者是读取文件、目录列表时所使用的编码,默认为UTF-8
        ///

        public Encoding Encode { get; set; }


        #endregion


        #region 构造函数
        public FtpHelper(Uri uri, string username, string password)
        {
            this.Uri = uri;
            this.UserName = username;
            this.Password = password;
            this.Encode = Encoding.GetEncoding("utf-8");
        }
        #endregion


        #region 建立连接
        ///


        /// 建立FTP链接,返回请求对象
        ///

        /// FTP地址
        /// 操作命令(WebRequestMethods.Ftp)
        ///
        private FtpWebRequest CreateRequest(Uri uri, string method)
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
                request.Credentials = new NetworkCredential(this.UserName, this.Password);//指定登录ftp服务器的用户名和密码。
                request.KeepAlive = false;//指定连接是应该关闭还是在请求完成之后关闭,默认为true
                request.UsePassive = true;//指定使用被动模式,默认为true
                request.UseBinary = true;//指示服务器要传输的是二进制数据.false,指示数据为文本。默认值为true
                request.EnableSsl = false;//如果控制和数据传输是加密的,则为true.否则为false.默认值为 false
                request.Method = method;
                return request;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        ///


        /// 建立FTP链接,返回响应对象
        ///

        /// FTP地址
        /// 操作命令(WebRequestMethods.Ftp)
        ///
        private FtpWebResponse CreateResponse(Uri uri, string method)
        {
            try
            {
                FtpWebRequest request = CreateRequest(uri, method);
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                this.StatusCode = response.StatusCode;
                this.StatusDescription = response.StatusDescription;
                return response;
            }
            catch (WebException ex)
            {
                FtpWebResponse response = ex.Response as FtpWebResponse;
                if (response != null)
                {
                    this.StatusCode = response.StatusCode;
                    this.StatusDescription = response.StatusDescription;
                }
                throw ex;
            }
        }
        #endregion


        #region 上传文件
        ///


        /// 上传文件到FTP服务器,若文件已存在自动覆盖
        /// 本方法不会自动创建远程路径的目录
        ///

        /// 本地带有完整路径的文件名
        /// 要在FTP服务器上面保存完整文件名
        public bool UploadFile(string localFilePath, string remoteFilePath)
        {
            return UploadFile(localFilePath, remoteFilePath, false);
        }
        ///
        /// 上传文件到FTP服务器,若文件已存在自动覆盖
        ///

        /// 本地带有完整路径的文件名
        /// 要在FTP服务器上面保存完整文件名
        /// 是否自动递归创建文件目录
        ///
        public bool UploadFile(string localFilePath, string remoteFilePath, bool autoCreateDirectory)
        {
            try
            {
                //自动递归创建目录
                if (autoCreateDirectory)
                {
                    if (!CreateDirectory(Path.GetDirectoryName(remoteFilePath)))
                    {
                        //递归创建目录失败,返回false
                        return false;
                    }
                }
                FileInfo fileInf = new FileInfo(localFilePath);
                if (!fileInf.Exists)
                {
                    throw new FileNotFoundException(string.Format("本地文件不存在:{0}!", localFilePath));
                }


                FtpWebRequest request = CreateRequest(new Uri(this.Uri + remoteFilePath), WebRequestMethods.Ftp.UploadFile);


                request.ContentLength = fileInf.Length;


                int contentLen = 0;
                //缓冲2kb
                byte[] buff = new byte[2048];
                using (FileStream fs = fileInf.OpenRead())
                {
                    using (Stream stream = request.GetRequestStream())
                    {
                        while ((contentLen = fs.Read(buff, 0, buff.Length)) > 0)
                        {
                            stream.Write(buff, 0, contentLen);
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                this.Exception = ex;
                this.ErrorMsg = ex.Message;
            }
            return false;
        }
        #endregion


        #region 下载文件
        ///


        /// 从FTP服务器下载文件
        ///

        /// 远程完整文件名
        /// 本地带有完整路径的文件名
        public bool DownloadFile(string remoteFilePath, string localFilePath)
        {
            try
            {
                string localDirector = Path.GetDirectoryName(localFilePath);
                if (!Directory.Exists(localDirector))
                {
                    Directory.CreateDirectory(localDirector);
                }


                FtpWebResponse response = CreateResponse(new Uri(this.Uri + remoteFilePath), WebRequestMethods.Ftp.DownloadFile);
                byte[] buffer = new byte[2048];
                int bytesCount = 0;
                Stream stream = response.GetResponseStream();
                using (FileStream fs = new FileStream(localFilePath, FileMode.Create))
                {
                    while ((bytesCount = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fs.Write(buffer, 0, bytesCount);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                this.Exception = ex;
                this.ErrorMsg = ex.Message;
            }
            return false;
        }
        #endregion


        #region 移动、重命名文件
        ///


        /// 移动远程文件文件
        ///

        /// 远程文件名
        /// 新文件名
        ///
        public bool MoveFile(string remoteFileName, string newFileName)
        {
            return ReName(remoteFileName, newFileName);
        }
        ///
        /// 重命名远程文件
        ///

        /// 远程文件名
        /// 新文件名
        ///
        public bool ReName(string remoteFileName, string newFileName)
        {
            try
            {
                if (remoteFileName != newFileName)
                {
                    FtpWebRequest request = CreateRequest(new Uri(this.Uri + remoteFileName), WebRequestMethods.Ftp.Rename);
                    request.RenameTo = newFileName;
                    request.GetResponse();
                }
                return true;
            }
            catch (WebException ex)
            {
                this.ErrorMsg = ex.Message;
                this.Exception = ex;
                FtpWebResponse response = ex.Response as FtpWebResponse;
                if (response != null)
                {
                    this.StatusCode = response.StatusCode;
                    this.StatusDescription = response.StatusDescription;
                }
            }
            return false;
        }
        #endregion


        #region 删除文件
        ///


        /// 删除远程文件
        ///

        ///
        /// 成功返回True,否则返回False
        public bool DeleteFile(string fileName)
        {
            try
            {
                CreateResponse(new Uri(this.Uri + fileName), WebRequestMethods.Ftp.DeleteFile);
                return true;
            }
            catch (Exception ex)
            {
                this.Exception = ex;
                this.ErrorMsg = ex.Message;
            }
            return false;
        }
        #endregion


        #region 递归创建目录
        ///


        /// 递归创建目录,在创建目录前不进行目录是否已存在检测
        ///

        ///
        public bool CreateDirectory(string remoteDirectory)
        {
            return CreateDirectory(remoteDirectory, false);
        }


        ///


        /// 在FTP服务器递归创建目录
        ///

        /// 要创建的目录
        /// 创建目录前是否进行目录是否存在检测
        ///
        public bool CreateDirectory(string remoteDirectory, bool autoCheckExist)
        {
            try
            {
                string parentDirector = "/";
                if (!string.IsNullOrEmpty(remoteDirectory))
                {
                    remoteDirectory = remoteDirectory.Replace("", "/");
                    string[] directors = remoteDirectory.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string director in directors)
                    {
                        if (!parentDirector.EndsWith("/")) parentDirector += "/";
                        if (autoCheckExist)
                        {
                            if (!DirectoryExist(parentDirector, director))
                                CreateResponse(new Uri(this.Uri + parentDirector + director), WebRequestMethods.Ftp.MakeDirectory);
                        }
                        else
                        {
                            try
                            {
                                CreateResponse(new Uri(this.Uri + parentDirector + director), WebRequestMethods.Ftp.MakeDirectory);
                            }
                            catch (WebException ex)
                            {
                                if (this.StatusCode != FtpStatusCode.ActionNotTakenFileUnavailable)
                                {
                                    throw ex;
                                }
                            }
                        }
                        parentDirector += director;
                    }
                }
                return true;
            }
            catch (WebException ex)
            {
                this.Exception = ex;
                this.ErrorMsg = ex.Message;
            }
            return false;
        }


        ///


        /// 检测指定目录下是否存在指定的目录名
        ///

        ///
        ///
        ///
        private bool DirectoryExist(string parentDirector, string directoryName)
        {
            List list = GetFileAndDirectoryList(parentDirector);
            foreach (FileStruct fstruct in list)
            {
                if (fstruct.IsDirectory && fstruct.Name == directoryName)
                {
                    return true;
                }
            }
            return false;
        }

 


        #endregion


        #region 检测文件是否已存在
        ///


        /// 检测FTP服务器上是否存在指定文件
        /// 中文文件名若存在无法正确检测现在有肯能是编码问题所致
        /// 请调用this.Encode进行文件编码设置,默认为UTF-8,一般改为GB2312就能正确识别
        ///

        ///
        ///
        public bool FileExist(string remoteFilePath)
        {
            List list = GetFileAndDirectoryList(Path.GetDirectoryName(remoteFilePath));
            foreach (FileStruct fstruct in list)
            {
                if (!fstruct.IsDirectory && fstruct.Name == Path.GetFileName(remoteFilePath))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion


        #region 目录、文件列表
        ///


        /// 获取FTP服务器上指定目录下的所有文件和目录
        /// 若获取的中文文件、目录名优乱码现象
        /// 请调用this.Encode进行文件编码设置,默认为UTF-8,一般改为GB2312就能正确识别
        ///

        ///
        ///
        public List GetFileAndDirectoryList(string direcotry)
        {
            try
            {
                List list = new List();
                string str = null;
                //WebRequestMethods.Ftp.ListDirectoryDetails可以列出所有的文件和目录列表
                //WebRequestMethods.Ftp.ListDirectory只能列出目录的文件列表
                FtpWebResponse response = CreateResponse(new Uri(this.Uri.ToString() + direcotry), WebRequestMethods.Ftp.ListDirectoryDetails);
                Stream stream = response.GetResponseStream();


                using (StreamReader sr = new StreamReader(stream, this.Encode))
                {
                    str = sr.ReadToEnd();
                }
                string[] fileList = str.Split(new char[] { 'r', 'n' }, StringSplitOptions.RemoveEmptyEntries);
                EFileListFormat format = JudgeFileListFormat(fileList);
                if (!string.IsNullOrEmpty(str) && format != EFileListFormat.Unknown)
                {
                    list = ParseFileStruct(fileList, format);
                }
                return list;
            }
            catch (WebException ex)
            {
                throw ex;
            }
        }


        ///


        /// 解析文件列表信息返回文件列表
        ///

        ///
        /// 文件列表格式
        ///
        private List ParseFileStruct(string[] fileList, EFileListFormat format)
        {
            List list = new List();
            if (format == EFileListFormat.UnixFormat)
            {
                foreach (string info in fileList)
                {
                    FileStruct fstuct = new FileStruct();
                    fstuct.Origin = info.Trim();
                    fstuct.OriginArr = fstuct.Origin.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (fstuct.OriginArr.Length == 9)
                    {
                        fstuct.Flags = fstuct.OriginArr[0];
                        fstuct.IsDirectory = (fstuct.Flags[0] == 'd');
                        fstuct.Owner = fstuct.OriginArr[2];
                        fstuct.Group = fstuct.OriginArr[3];
                        fstuct.Size = Convert.ToInt32(fstuct.OriginArr[4]);
                        if (fstuct.OriginArr[7].Contains(":"))
                        {
                            fstuct.OriginArr[7] = DateTime.Now.Year + " " + fstuct.OriginArr[7];
                        }
                        fstuct.UpdateTime = DateTime.Parse(string.Format("{0} {1} {2}", fstuct.OriginArr[5], fstuct.OriginArr[6], fstuct.OriginArr[7]));
                        fstuct.Name = fstuct.OriginArr[8];
                        if (fstuct.Name != "." && fstuct.Name != "..")
                        {
                            list.Add(fstuct);
                        }
                    }


                }
            }
            else if (format == EFileListFormat.WindowsFormat)
            {
                foreach (string info in fileList)
                {
                    FileStruct fstuct = new FileStruct();
                    fstuct.Origin = info.Trim();
                    fstuct.OriginArr = fstuct.Origin.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (fstuct.OriginArr.Length == 4)
                    {
                        DateTimeFormatInfo usDate = new CultureInfo("en-US", false).DateTimeFormat;
                        usDate.ShortTimePattern = "t";
                        fstuct.UpdateTime = DateTime.Parse(fstuct.OriginArr[0] + " " + fstuct.OriginArr[1], usDate);


                        fstuct.IsDirectory = (fstuct.OriginArr[2] == "

");
                        if (!fstuct.IsDirectory)
                        {
                            fstuct.Size = Convert.ToInt32(fstuct.OriginArr[2]);
                        }
                        fstuct.Name = fstuct.OriginArr[3];
                        if (fstuct.Name != "." && fstuct.Name != "..")
                        {
                            list.Add(fstuct);
                        }
                    }
                }
            }
            return list;
        }


        ///


        /// 判断文件列表的方式Window方式还是Unix方式
        ///

        /// 文件信息列表
        ///
        private EFileListFormat JudgeFileListFormat(string[] fileList)
        {
            foreach (string str in fileList)
            {
                if (str.Length > 10 && Regex.IsMatch(str.Substring(0, 10), "(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)"))
                {
                    return EFileListFormat.UnixFormat;
                }
                else if (str.Length > 8 && Regex.IsMatch(str.Substring(0, 8), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]"))
                {
                    return EFileListFormat.WindowsFormat;
                }
            }
            return EFileListFormat.Unknown;
        }


        private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
        {
            FileStruct f = new FileStruct();
            string processstr = Record.Trim();
            string dateStr = processstr.Substring(0, 8);
            processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();
            string timeStr = processstr.Substring(0, 7);
            processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();
            DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat;
            myDTFI.ShortTimePattern = "t";
            f.UpdateTime = DateTime.Parse(dateStr + " " + timeStr, myDTFI);
            if (processstr.Substring(0, 5) == "

")
            {
                f.IsDirectory = true;
                processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
            }
            else
            {
                string[] strs = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);   // true);
                processstr = strs[1];
                f.IsDirectory = false;
            }
            f.Name = processstr;
            return f;
        }
        #endregion
    }


    #region 文件结构
    ///


    /// 文件列表格式
    ///

    public enum EFileListFormat
    {
        ///
        /// Unix文件格式
        ///

        UnixFormat,
        ///
        /// Window文件格式
        ///

        WindowsFormat,
        ///
        /// 未知格式
        ///

        Unknown
    }


    public struct FileStruct
    {
        public string Origin { get; set; }
        public string[] OriginArr { get; set; }
        public string Flags { get; set; }
        ///


        /// 所有者
        ///

        public string Owner { get; set; }
        public string Group { get; set; }
        ///
        /// 是否为目录
        ///

        public bool IsDirectory { get; set; }
        ///
        /// 文件或目录更新时间
        ///

        public DateTime UpdateTime { get; set; }
        ///
        /// 文件或目录名称
        ///

        public string Name { get; set; }


        ///


        /// 文件大小(目录始终为0)
        ///

        public int Size { get; set; }
    }
    #endregion


}

源码下载地址

http://down.111com.net/down/code/net/2011/0612/23160.html

相关文章

精彩推荐