zoukankan      html  css  js  c++  java
  • (转)FTP操作类,从FTP下载文件

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Windows.Forms;
    
    namespace PCMDataImporter
    {
        /// <summary>
        /// 封装了FTP的两个操作:下载文件Download()和获取FTP服务器上文件列表信息GetFileList()
        /// </summary>
        public class ftpOperater
        {
            private string ftpServerIP;
            private string ftpUser;
            private string ftpPwd;
    
            private SystemParaReader spReader;
    
            public ftpOperater()
            {
                spReader = new SystemParaReader();
                this.ftpServerIP = spReader.GetSysParaValue("ftpServer");
                this.ftpUser = spReader.GetSysParaValue("ftpUser");
                this.ftpPwd = spReader.GetSysParaValue("ftpPwd");
            }
    
            /// <summary>
            /// 获取ftp服务器上的文件信息
            /// </summary>
            /// <returns>存储了所有文件信息的字符串数组</returns>
            public string[] GetFileList()
            {
                string[] downloadFiles;
                StringBuilder result = new StringBuilder();
                FtpWebRequest reqFTP;
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                    WebResponse response = reqFTP.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream());
    
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("
    ");
                        line = reader.ReadLine();
                    }
                    result.Remove(result.ToString().LastIndexOf('
    '), 1);
                    reader.Close();
                    response.Close();
    
                    return result.ToString().Split('
    ');
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("获取文件信息失败:"+ex.Message,"操作失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    downloadFiles = null;
                    return downloadFiles;
                }
            }
    
            /// <summary>
            /// 获取FTP上指定文件的大小
            /// </summary>
            /// <param name="filename">文件名</param>
            /// <returns>文件大小</returns>
            public long GetFileSize(string filename)
            {
                FtpWebRequest reqFTP;
                long fileSize = 0;
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + filename));
                    reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    Stream ftpStream = response.GetResponseStream();
                    fileSize = response.ContentLength;
    
                    ftpStream.Close();
                    response.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("获取文件大小时,出现异常:
    " + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return fileSize;
            }
    
            /// <summary>
            /// 实现ftp下载操作
            /// </summary>
            /// <param name="filePath">保存到本地的文件名</param>
            /// <param name="fileName">远程文件名</param>
            public void Download(string filePath, string fileName)
            {
                FtpWebRequest reqFTP;
                try
                {
                    //filePath = <<The full path where the file is to be created.>>, 
                    //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
                    FileStream outputStream = new FileStream(filePath + "\" + fileName, FileMode.Create);
    
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    Stream ftpStream = response.GetResponseStream();
                    long cl = 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();
                    outputStream.Close();
                    response.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
    
  • 相关阅读:
    Misha and Changing Handles
    Garland CodeForces
    Hyperset
    Common Prefixes
    vue笔记整理
    react学习笔记一些整理
    react和vue项目总结
    Vue+elementUi《后台管理系统》前端实现02
    Vue+elementUi《后台管理系统》后台接口准备01
    React+AntdUi实现《好客租房系统》发布房源08
  • 原文地址:https://www.cnblogs.com/candyzhmm/p/5692879.html
Copyright © 2011-2022 走看看