实现了下载文件Download()与获取FTP服务器上文件列表信息GetFileList()

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.IO;
  5 using System.Net;
  6 using System.Windows.Forms;
  7 
  8 namespace PCMDataImporter
  9 {
 10     /// <summary>
 11     /// 封装了FTP的两个操作:下载文件Download()和获取FTP服务器上文件列表信息GetFileList()
 12     /// </summary>
 13     public class ftpOperater
 14     {
 15         private string ftpServerIP;
 16         private string ftpUser;
 17         private string ftpPwd;

 18 
 19         private SystemParaReader spReader;
 20 
 21         public ftpOperater()
 22         {
 23             spReader = new SystemParaReader();
 24             this.ftpServerIP = spReader.GetSysParaValue("ftpServer");
 25             this.ftpUser = spReader.GetSysParaValue("ftpUser");
 26             this.ftpPwd = spReader.GetSysParaValue("ftpPwd");
 27         }
 28 
 29         /// <summary>
 30         /// 获取ftp服务器上的文件信息
 31         /// </summary>
 32         /// <returns>存储了所有文件信息的字符串数组</returns>
 33         public string[] GetFileList()
 34         {
 35             string[] downloadFiles;
 36             StringBuilder result = new StringBuilder();
 37             FtpWebRequest reqFTP;
 38             try
 39             {
 40                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
 41                 reqFTP.UseBinary = true;
 42                 reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
 43                 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
 44                 WebResponse response = reqFTP.GetResponse();
 45                 StreamReader reader = new StreamReader(response.GetResponseStream());
 46 
 47                 string line = reader.ReadLine();
 48                 while (line != null)
 49                 {
 50                     result.Append(line);
 51                     result.Append("\n");
 52                     line = reader.ReadLine();
 53                 }
 54                 result.Remove(result.ToString().LastIndexOf('\n'), 1);
 55                 reader.Close();
 56                 response.Close();
 57 
 58                 return result.ToString().Split('\n');
 59             }
 60             catch (Exception ex)
 61             {
 62                 System.Windows.Forms.MessageBox.Show("获取文件信息失败:"+ex.Message,"操作失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
 63                 downloadFiles = null;
 64                 return downloadFiles;
 65             }
 66         }
 67 
 68         /// <summary>
 69         /// 获取FTP上指定文件的大小
 70         /// </summary>
 71         /// <param name="filename">文件名</param>
 72         /// <returns>文件大小</returns>
 73         public long GetFileSize(string filename)
 74         {
 75             FtpWebRequest reqFTP;
 76             long fileSize = 0;
 77             try
 78             {
 79                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + filename));
 80                 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
 81                 reqFTP.UseBinary = true;
 82                 reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
 83                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
 84                 Stream ftpStream = response.GetResponseStream();
 85                 fileSize = response.ContentLength;
 86 
 87                 ftpStream.Close();
 88                 response.Close();
 89             }
 90             catch (Exception ex)
 91             {
 92                 MessageBox.Show("获取文件大小时,出现异常:\n" + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
 93             }
 94             return fileSize;
 95         }
 96 
 97         /// <summary>
 98         /// 实现ftp下载操作
 99         /// </summary>
100         /// <param name="filePath">保存到本地的文件名</param>
101         /// <param name="fileName">远程文件名</param>
102         public void Download(string filePath, string fileName)
103         {
104             FtpWebRequest reqFTP;
105             try
106             {
107                 //filePath = <<The full path where the file is to be created.>>,
108                 //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
109                 FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
110 
111                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
112                 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
113                 reqFTP.UseBinary = true;
114                 reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
115                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
116                 Stream ftpStream = response.GetResponseStream();
117                 long cl = response.ContentLength;
118                 int bufferSize = 2048;
119                 int readCount;
120                 byte[] buffer = new byte[bufferSize];
121 
122                 readCount = ftpStream.Read(buffer, 0, bufferSize);
123                 while (readCount > 0)
124                 {
125                     outputStream.Write(buffer, 0, readCount);
126                     readCount = ftpStream.Read(buffer, 0, bufferSize);

127                 }
128 
129                 ftpStream.Close();
130                 outputStream.Close();
131                 response.Close();
132             }
133             catch (Exception ex)
134             {
135                 MessageBox.Show(ex.Message);
136             }
137         }
138     }
139 }
140  

 

posted @ 2014-08-12 15:20  xfyn  阅读(1265)  评论(0编辑  收藏  举报