当前位置:编程学习 > C#/ASP.NET >>

下载ftp上某文件夹下所有的文件

 /// <summary>
        /// 下载ftp上文件
        /// </summary>
        /// <param name="fileOnFtpPath">ftp上的文件路径</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="savePath">保存到本地文件的路径</param>
        private static void Download(string fileOnFtpPath, string fileName, string savePath)
        {
            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(savePath + "\\" + fileName, FileMode.Create);
                //reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileOnFtpPath + "/" + fileName));
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + fileOnFtpPath + "/" + fileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(user, pwd);
                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)
            {
                throw ex;
            }
        }


        public static void SavePolicyAndDocumentInterface()
        {
            
            TransactionContext tc = new TransactionContext();
            //下载文件
            try
            {
                //下载后的文件所保存的路径
                string savepath = Setting.DataDirectory + "\\ServiceLog\\Psell\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day;
                DirectoryInfo di = new DirectoryInfo(savepath);
                if (!di.Exists)
                {
                    System.IO.Directory.CreateDirectory(savepath);
                }
                //获得昨天的时间
                DateTime yestoday = DateTime.Now.AddDays(-1);
                //ftp上文件的路径
                String fileOnFtpPath = "Psell/" + yestoday.Year + "/" + yestoday.Month + "/" + yestoday.Day;
                
                Download(fileOnFtpPath,"XXX.txt", savepath);
         }
   }

代码是这样写的,我要怎么下载yestoday.Day下面有所有文件,在Download(fileOnFtpPath,"XXX.txt", savepath);方法里,"XXX.txt"是以参数传进去,而不是写死的!  --------------------编程问答--------------------

sing System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Diagnostics;
namespace AutoUpData.FtpUpData
{
   public class FtpUpdata 
    {
       
        Entity et = new Entity();
        FtpWebRequest reqFTP;

        private void Connect(String path)//连接ftp
        {
            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
            // 指定数据传输类型
            reqFTP.UseBinary = true;
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(et.FtpUserID,et.FtpPassWord);
        }

        public void FtpUpDown(Entity _et)
        {
            et = _et;
        }
        //都调用这个
        private string[] GetFileList(string path, string WRMethods)//上面的代码示例了如何从ftp服务器上获得文件列表
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            try
            {
                Connect(path);
                reqFTP.Method = WRMethods;
                WebResponse response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名
                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append(" ");
                    line = reader.ReadLine();
                }
                // to remove the trailing " "
                result.Remove(result.ToString().LastIndexOf(" "), 1);
                reader.Close();
                response.Close();
                return result.ToString().Split();
            }
            catch (Exception ex)
            {
                wl.WhatToDo("FG0001",ex.ToString());
                downloadFiles = null;
                return downloadFiles;
            }
        }

        public string[] GetFileList(string path)//上面的代码示例了如何从ftp服务器上获得文件列表
        {
            return GetFileList("ftp://" + et.FtpAddress + "/" + path, WebRequestMethods.Ftp.ListDirectory);
        }

        public string[] GetFileList()//上面的代码示例了如何从ftp服务器上获得文件列表
        {
            return GetFileList("ftp://" + et.FtpAddress + "/", WebRequestMethods.Ftp.ListDirectory);
        }
        //上面的代码实现了从ftp服务器下载文件的功能
        public bool Download(string filePath, string fileName)
        {
            try
            {
                String onlyFileName = Path.GetFileName(fileName);
                string newFileName = filePath + "\\" + onlyFileName;
                if (File.Exists(newFileName))
                {
                    File.Delete(newFileName);
                    //errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName);
                    //return false;
                }
                string url = "ftp://" + et.FtpAddress + "/" + fileName;
                Connect(url);//连接 
                reqFTP.Credentials = new NetworkCredential(et.FtpUserID, et.FtpPassWord);
                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);
                FileStream outputStream = new FileStream(newFileName, FileMode.Create);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
                return true;
            }
            catch (Exception ex)
            {
               string errorinfo = string.Format("因{0},无法下载", ex.Message);
                wl.WhatToDo("FD0001",errorinfo);
                return false;
            }
        }
/*--------------------调用-----------------------------*/
private bool Downlist(ftp地址)//如 192.168.1.1  192.168.1.1/文件夹名/...
{
try{
string [] files = GetFileList(ftp地址);
foreach(string file in files)
{
return Download(ftp地址,file);
}}
catch{
return false;}
}


补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,