当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-08-30

    本文导语:  asp.net ftp操作类。   代码示例: public class FTP {         ///         /// 从Ftp上下载文件         ///         /// 文件下载后的保存路径         /// 源图片名称         /// 本地保存文件名称         /// ...

asp.net ftp操作类。
 

代码示例:
public class FTP
{
        ///
        /// 从Ftp上下载文件
        ///
        /// 文件下载后的保存路径
        /// 源图片名称
        /// 本地保存文件名称
        /// ftp地址
        /// ftp上的文件夹名称
        /// 访问ftp的用户名
        /// 访问ftp的密码
        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();
            }
        }
 
        ///
        /// 删除服务器上的文件
       /// by www.
        ///
        /// 文件路径
        public void DeleteServerFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
 
        ///
        /// 删除Ftp上的文件
        ///
        /// 删除文件名称集合
        /// 被删除文件所属的文件夹
        /// ftp地址
        /// ftp访问用户名
        /// ftp访问密码
        private void DeleteFtpFile(string[] Names, string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
        {
            foreach (string imageName in Names)
            {
                List 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();
                        }
                    }
                }
            }
        }
 
        ///
        /// 获取特定文件夹中的文件
        ///
        /// 文件夹名称
        /// ftp地址
        /// 访问ftp的
        /// 访问ftp的密码
        /// 返回特定文件夹中的文件名称列表
        public List GetFileList(string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
        {
            List downFiles = new List(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();
        }
    }

    
 
 

您可能感兴趣的文章:

  • 请问ASP转JSP大概要多长时间?有快速入门方法吗?
  • 我是编写ASP程序的,现在想学JAVA程序,请介绍几本入门书!提者有分!
  • asp.net 验证码的示例代码(入门参考)
  • c#/ASP.NET操作cookie(读写)代码示例
  • Linux操作系统能运行Asp.net的项目吗?
  • asp.net操作cookie的例子
  • 用ASP实现对Oracle数据库的操作
  • asp.net操作cookie实例代码
  • asp.net 操作cookie实例详解
  • c#(asp.net) 时间操作基类(支持长短日期与时间差)
  • 一个ASP.NET的MYSQL的数据库操作类自己封装的
  • asp.net获取客户端参数与操作系统信息
  • asp.net xml文件的读写、添加、修改、删除操作示例
  • asp.net 操作cookie的简单实例
  • ASP.net中获取客户端参数操作系统信息
  • asp.net操作cookie的代码
  • asp.net操作ini文件示例
  • asp.net操作xml增删改示例分享
  • ASP.NET下向SQLServer2008导入文件实例操作方法
  • asp.net操作cookie详解
  • asp.net 操作config配置文件的方法介绍
  • asp.net xml操作实例小结
  • asp.net cookie操作实例学习
  • c#(asp.net) excel操作实例
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • win2008 r2 服务器环境配置(FTP/ASP/ASP.Net/PHP)
  • ASP.NET之 Ajax相关知识介绍及组件图
  • 我想了解一些关于Java怎样与Asp或Asp.net结合方面在未来发展方向的问题?
  • asp.net UrlEncode对应asp urlencode的处理方法
  • asp.net实例 定义和使用asp:AccessDataSource
  • asp与asp.net的session共享
  • 如何在unix下发布asp?
  • 怎么让Apache支持Asp?
  • ??谁能把ASP代码改为JSP的
  • Linux平台下哪种方法实现ASP好?
  • ASP和ASP.Net共享Session解决办法
  • 通过socket和asp打交道
  • 犹豫中……,到底是选择ASP,还是JSP?
  • asp 是否 可用applet标签?帮忙!!
  • asp.net判断数据库表是否存在 asp.net修改表名的方法
  • 新人提问:asp+access的程序在linux下怎么改?
  • 用JAVA APPLET做的交互式网页和ASP、PHP做的相比有什么优势呢?
  • asp.net操作ini文件示例 iis7站长之家
  • asp里面可否使用java写的邮件,给30分.
  • asp与Jsp可否在iis中共存的问题
  • 实现Asp与Asp.Net共享Session的方法
  • 在ASP 有SPLIT ;JSP里怎样写


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3