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;
            }
        }
  • 相关阅读:
    存储过程与触发器的区别
    WebDriver基本操作入门及UI自动化练手页面
    第四章 TestNG测试用例分步解析(上)
    第三章 Webdriver Java API简介(下)
    第三章 Webdriver Java API简介(上)
    第二章 TestNG环境搭建
    第一章 TestNG框架自动化简述
    基于Selenium2和TestNG的自动化测试
    程序员都应该知道的福利
    TestNG系列教程:并行执行测试
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/6909228.html
Copyright © 2011-2022 走看看