技术改变世界!学习改变自己!

每天进步一点点,遥不可及的事情,在你一点一滴的努力下,也会变成现实。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  55 随笔 :: 0 文章 :: 33 评论 :: 0 引用

项目中用到的一个FTP操作类,实现了:下载文件Download()和获取FTP服务器上文件列表信息GetFileList()

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(
"\n");
                    line 
= reader.ReadLine();
                }
                result.Remove(result.ToString().LastIndexOf(
'\n'), 1);
                reader.Close();
                response.Close();

                
return result.ToString().Split('\n');
            }
            
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(
"获取文件大小时,出现异常:\n" + 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);
            }
        }
    }
}


 

posted on 2010-05-11 15:28 阿捷 阅读(...) 评论(...) 编辑 收藏