asp.net Ftp操作类(简单入门)

发布时间:2019-11-20编辑:脚本学堂
为大家提供一个简单的ftp操作类,通过ftp实现文件的上传与下载,有需要的朋友,可以参考下。

asp.net ftp操作类。
 

复制代码 代码示例:
public class FTP
{
        /// <summary>
        /// 从Ftp上下载文件
        /// </summary>
        /// <param name="saveFilePath">文件下载后的保存路径</param>
        /// <param name="imageSrc">源图片名称</param>
        /// <param name="imageName">本地保存文件名称</param>
        /// <param name="ftpAddress">ftp地址</param>
        /// <param name="folderName">ftp上的文件夹名称</param>
        /// <param name="ftpUserName">访问ftp的用户名</param>
        /// <param name="ftpPwd">访问ftp的密码</param>
        public void DownLoad(string saveFilePath, string imageSrc, string imageName, string ftpAddress,string folderName, string ftpUserName, string ftpPwd)
        {
            if (!Directory.Exists(saveFilePath))
            {
                Directory.CreateDirectory(saveFilePath);
            }
 
            using (FileStream outputStream = new FileStream(saveFilePath + "" + imageName, FileMode.Create))
            {
                FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + imageSrc));
                reqFtp.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFtp.UseBinary = true;
 
                //ftp访问通行证
                reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
 
                using (FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse())
                {
                    using (Stream ftpStream = response.GetResponseStream())
                    {
                        long len = response.ContentLength;
                        int bufferSize = 2048;
                        int readCount;
                        byte[] buffer = new byte[bufferSize];
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
 
                        while (readCount > 0)
                        {
                            outputStream.Write(buffer, 0, readCount);
                            readCount = ftpStream.Read(buffer, 0, bufferSize);
                        }
                        ftpStream.Close();
                    }
                    response.Close();
                }
                outputStream.Close();
            }
        }
 
        public void Uplaod(string saveFileDsPath, string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
        {
            FileInfo fileInfo = new FileInfo(saveFileDsPath);
            FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + fileInfo.Name));
            reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
            reqFtp.KeepAlive = false;
            reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
            reqFtp.UseBinary = true;
            reqFtp.ContentLength = fileInfo.Length;
            int buffSize = 2048;
            byte[] buff = new byte[buffSize];
            int contentLen = 0;
 
            using (FileStream fs = fileInfo.OpenRead())
            {
                using (Stream stream = reqFtp.GetRequestStream())
                {
                    contentLen = fs.Read(buff, 0, buffSize);
                    while (contentLen != 0)
                    {
                        stream.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffSize);
                    }
                    stream.Close();
                }
                fs.Close();
            }
        }
 
        /// <summary>
        /// 删除服务器上的文件
       /// by www.jb200.com
        /// </summary>
        /// <param name="filePath">文件路径</param>
        public void DeleteServerFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
 
        /// <summary>
        /// 删除Ftp上的文件
        /// </summary>
        /// <param name="Names">删除文件名称集合</param>
        /// <param name="folderName">被删除文件所属的文件夹</param>
        /// <param name="ftpAddress">ftp地址</param>
        /// <param name="ftpUserName">ftp访问用户名</param>
        /// <param name="ftpPwd">ftp访问密码</param>
        private void DeleteFtpFile(string[] Names, string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
        {
            foreach (string imageName in Names)
            {
                List<string> fileList = GetFileList(folderName, ftpAddress, ftpUserName, ftpPwd);
                foreach (var name in fileList)
                {
                    if (name == imageName)
                    {
                        FtpWebRequest reqFtp;
                        reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + imageName));
                        reqFtp.Credentials = new NetworkCredential(ftpUserName,ftpPwd);
                        reqFtp.KeepAlive = false;
                        reqFtp.Method = WebRequestMethods.Ftp.DeleteFile;
                        reqFtp.UseBinary = true;
 
                        using(FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse())
                        {
                            long size = response.ContentLength;
                            using(Stream dataStream = response.GetResponseStream())
                            {
                                using(StreamReader sr = new StreamReader(dataStream))
                                {
                                    sr.ReadToEnd();
                                    sr.Close();
                                }
                                dataStream.Close();
                            }
                            response.Close();
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// 获取特定文件夹中的文件
        /// </summary>
        /// <param name="folderName">文件夹名称</param>
        /// <param name="ftpAddress">ftp地址</param>
        /// <param name="ftpUserName">访问ftp的</param>
        /// <param name="ftpPwd">访问ftp的密码</param>
        /// <returns>返回特定文件夹中的文件名称列表</returns>
        public List<string> GetFileList(string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
        {
            List<string> downFiles = new List<string>(4);
            FtpWebRequest reqFtp;
 
            reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/"));
            reqFtp.UseBinary = true;
            reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
            reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
            WebResponse response = reqFtp.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
 
            string line = reader.ReadLine();
            while (line != null)
            {
                downFiles.Add(line);
                line = reader.ReadLine();
            }
 
            reader.Close();
            response.Close();
 
            //获取图片名称为:1.jpg、2.jpg、3.jpg
            var FileList = from file in downFiles
                        let tempname = file.Substring(0, file.IndexOf('.'))
                        where tempname == "1" || tempname == "2" || tempname == "3"
                        select file;
 
            return FileList.ToList();
        }
    }