一个开源的Asp.net2.0博客系统
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.IO;
- using System.Windows.Forms;
- using System.Text;
- namespace FTPDownLoad
- {
- class FtpFileInfo
- {
- public string FileName;
- public DateTime ModifyDateTime;
- public bool IsDirectory;
- }
- class FtpHelper
- {
- private string ftpServer;
- private string userName;
- private string password;
- FtpWebRequest ftpRequest = null;
- private string errMsg;
- public string ErrMsg
- {
- get { return errMsg; }
- set { errMsg = value; }
- }
- public bool IsAnonymous
- {
- get
- {
- return !(userName != null && userName.Trim() != String.Empty
- && password != null && password.Trim() != string.Empty);
- }
- }
- public FtpHelper(string ftpServer, string userName, string password)
- {
- this.ftpServer = ftpServer;
- this.userName = userName;
- this.password = password;
- }
- /**/
- /// <summary>
- /// 取得服务器端的文件链表
- /// </summary>
- /// <param name="serverPath"></param>
- /// <returns></returns>
- public List<FtpFileInfo> GetFilesList(string serverPath)
- {
- List<FtpFileInfo> fileInfoList = new List<FtpFileInfo>();
- Uri uri = new Uri("ftp://" + ftpServer + serverPath);
- StreamReader sr = null;
- try
- {
- ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uri);
- if (ftpRequest == null) throw new Exception("无法打开ftp服务器连接");
- ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; //列表
- if (!IsAnonymous)
- {
- ftpRequest.Credentials = new NetworkCredential(userName, password);
- }
- sr = new StreamReader(ftpRequest.GetResponse().GetResponseStream());
- while (!sr.EndOfStream)//读取列表
- {
- //System.Diagnostics.Debug.WriteLine(sr.ReadLine());
- char[] splitChar = { ' ' };
- string[] tmpArray = sr.ReadLine().Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
- if (tmpArray.Length != 9)
- {
- continue;
- }
- FtpFileInfo ffi = new FtpFileInfo();
- ffi.IsDirectory = tmpArray[0].StartsWith("d");
- if (!ffi.IsDirectory)
- {
- ffi.FileName = tmpArray[8];
- fileInfoList.Add(ffi);
- }
- else
- {
- continue;
- }
- }
- }
- catch (Exception ex)
- {
- //TODO: 异常处理.
- throw ex;
- }
- finally
- {
- if (sr != null) sr.Close();
- }
- foreach (FtpFileInfo ffi in fileInfoList)
- {
- ffi.ModifyDateTime = this.GetFileModifyTime(serverPath, ffi.FileName);
- }
- return fileInfoList;
- }
- /**/
- /// <summary>
- /// Download
- /// </summary>
- /// <param name="serverPath"></param>
- /// <param name="serverFileName"></param>
- /// <param name="localPath"></param>
- /// <param name="localFileName"></param>
- /// <returns></returns>
- public bool Download(string serverPath, string serverFileName, string localPath, string localFileName)
- {
- if (!Directory.Exists(localPath))
- {
- //TODO: 创建文件夹
- errMsg = "本地路径不存在: " + localPath;
- return false;
- }
- FileStream outputStream = null;
- Stream ftpStream = null;
- try
- {
- outputStream = new FileStream(localPath + "\\" + localFileName, FileMode.Create);
- if (outputStream == null)
- {
- errMsg = "无法创建本地文件";
- return false;
- }
- ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServer + serverPath + "/" + serverFileName));
- if (ftpRequest == null)
- {
- errMsg = "无法连接服务器";
- return false;
- }
- ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
- ftpRequest.UseBinary = true;
- //用户验证
- if (!IsAnonymous)
- {
- ftpRequest.Credentials = new NetworkCredential(userName, password);
- }
- ftpStream = ftpRequest.GetResponse().GetResponseStream();
- int bufferSize = 2048;
- int readCount;
- byte[] buffer = new byte[bufferSize];
- while ((readCount = ftpStream.Read(buffer, 0, bufferSize)) > 0)
- {
- outputStream.Write(buffer, 0, readCount);
- }
- }
- catch (Exception ex)
- {
- errMsg = ex.ToString();
- return false;
- }
- finally
- {
- if (ftpStream != null) ftpStream.Close();
- if (outputStream != null) outputStream.Close();
- }
- FileInfo fi = new FileInfo(localPath + "\\" + localFileName);
- fi.LastWriteTime = GetFileModifyTime(serverPath, serverFileName);
- return true;
- }
- public void DownLoadDirectory(String ServerPath, String LocalPath)
- {
- //取服务器端要下载的目录里的文件列表
- List<FtpFileInfo> serFileList = GetFilesList(ServerPath);
- foreach (FtpFileInfo ftpFile in serFileList)
- {
- if (ftpFile.IsDirectory) continue;
- DateTime modifyTime = ftpFile.ModifyDateTime;
- string localFileName = modifyTime.ToString("yyyyMMddHHmmss") + "_" + ftpFile.FileName;
- // 如果本地不存在该文件,则说明该文件是比较新的文件,需要下载.
- if (!File.Exists(LocalPath + "\\" + localFileName))
- {
- if (!Download(ServerPath, ftpFile.FileName, LocalPath, localFileName))
- {
- MessageBox.Show("下载文件出错!\r\n错误原因: " + ErrMsg);
- }
- }
- }
- }
- public DateTime GetFileModifyTime(string serverPath, string fileName)
- {
- Uri uri = new Uri("ftp://" + ftpServer + serverPath + "/" + fileName);
- DateTime dt = DateTime.Now;
- try
- {
- ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
- if (!IsAnonymous)
- {
- ftpRequest.Credentials = new NetworkCredential(userName, password);
- }
- ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = false;
- dt = ((FtpWebResponse)ftpRequest.GetResponse()).LastModified;
- }
- catch (Exception ex)
- {
- //TODO: 错误处理
- throw ex;
- }
- return dt;
- }
- }
- }