zoukankan      html  css  js  c++  java
  • C# FTPHelper工具类

        /// <summary>
        /// Ftp
        /// </summary>
        public class FtpFileOperation
        {
            private string _ftpIp;
            private string _ftpUser;
            private string _ftpPassword;
            private FtpWebRequest _ftpWebRequest;
    
            public FtpFileOperation(string ftpIp, string ftpUser, string ftpPassword)
            {
                _ftpIp = ftpIp;
                _ftpUser = ftpUser;
                _ftpPassword = ftpPassword;
            }
    
            /// <summary>
            /// Connect
            /// </summary>
            /// <param name="ftpUrl"></param>
            /// <returns></returns>
            public bool Connect(string ftpUrl)
            {
                try
                {
                    _ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(ftpUrl);
                    _ftpWebRequest.UseBinary = true;
                    _ftpWebRequest.Credentials = new NetworkCredential(_ftpUser, _ftpPassword);
                    return true;
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                    return false;
                }
            }
    
            /// <summary>
            /// Download
            /// </summary>
            /// <param name="ftpFilePath"></param>
            /// <param name="savePath"></param>
            /// <returns></returns>
            public bool Download(string ftpFilePath,string savePath)
            {
                try
                {
                    if (Connect(String.Format("ftp://{0}/{1}", _ftpIp, ftpFilePath)))
                    {
                        FtpWebResponse ftpResponse = (FtpWebResponse)_ftpWebRequest.GetResponse();
                        Stream ftpStream = ftpResponse.GetResponseStream();
                        byte[] buffer = new byte[2048];
                        int readCount = 0;
                        FileStream outputStream = new FileStream(savePath, FileMode.Create);
                        readCount = ftpStream.Read(buffer, 0, buffer.Length);
                        while(readCount > 0)
                        {
                            outputStream.Write(buffer, 0, readCount);
                            readCount = ftpStream.Read(buffer, 0, buffer.Length);
                        }
                        ftpStream.Close();
                        outputStream.Close();
                        ftpResponse.Close();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                }
                return false;
            }
        }
  • 相关阅读:
    汉字获取首字母
    .net 实现对DNS服务器的管理
    css使图片变灰
    javascript实现文本框只能输入数字和字母
    解决Outlook不能打开的问题
    javascript实现弹出式登录界面
    asp.net防盗链技术
    javascript中replace()(转帖)
    chm文件无法显示问题
    使用Lucene.NET进行分词、搜索
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/6909228.html
Copyright © 2011-2022 走看看