class DirectoryItem
{
public Uri BaseUri;
public string AbsolutePath
{ get { return string.Format("{0}/{1}", BaseUri, Name); } }
public DateTime DateCreated;
public bool IsDirectory;
public string Name;
public List<DirectoryItem> Items;
public override string ToString() { return Name; }
} internal enum enumFolderListFMT { UNIX, DOS_IIS };
internal enum enumFTPPlatform { WindowsServer2008, ServU}
/// <summary>
/// 获取目录信息(包含文件夹,文件)
/// </summary>
/// <param name="address"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
static List<DirectoryItem> GetDirectoryInformation(string addr2, string username, string password)
{
var address = addr2.EndsWith("/") ? addr2.Substring(, addr2.Length - ) : addr2;//去除最后一个斜杠
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false; string[] list = null;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
list = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
} //unix or dos_iis format?
enumFolderListFMT folderFormat = enumFolderListFMT.UNIX;
int dir_pos = ;
bool found = false;
foreach (var item in list)
{
if (item.ToLower().Contains("<dir>"))
{
folderFormat = enumFolderListFMT.DOS_IIS;
dir_pos = item.ToLower().IndexOf("<dir>");
found = true;
break;
}
}
if (!found && list.Length > && list[].ToLower()[] != 'd' && list[].ToLower()[] != '-')
{
folderFormat = enumFolderListFMT.DOS_IIS;
} enumFTPPlatform ftpPlatform = enumFTPPlatform.WindowsServer2008;
if (folderFormat == enumFolderListFMT.UNIX)
{
if (list.Length > && list[].Substring(, ).ToLower().Count(c => c == '-') < )
ftpPlatform = enumFTPPlatform.WindowsServer2008;
else
ftpPlatform = enumFTPPlatform.ServU;
} List<DirectoryItem> returnValue = new List<DirectoryItem>();
if (folderFormat == enumFolderListFMT.DOS_IIS)
{
foreach (var item in list)
{
if (item.ToLower().Contains("<dir>"))
{
var dir = item.Substring(dir_pos + ).Trim();
if (dir == "." || dir == "..") continue; var di = new DirectoryItem();
di.BaseUri = new Uri(address);
//di.DateCreated = dateTime;
di.IsDirectory = true;
di.Name = dir;
//Debug.WriteLine(di.AbsolutePath);
di.Items = GetDirectoryInformation(di.AbsolutePath, username, password);
returnValue.Add(di);
}
else
{
string filename = "";
if(found)
filename = item.Substring(dir_pos + ).Trim();
else
filename = item.Substring().Trim();
var di = new DirectoryItem();
di.BaseUri = new Uri(address);
di.IsDirectory = false;
di.Name = filename;
di.Items = null;
returnValue.Add(di);
}
}
}
else if (folderFormat == enumFolderListFMT.UNIX)
{
var pos = ftpPlatform == enumFTPPlatform.WindowsServer2008 ? : ;
foreach (var item in list)
{
if (item.Substring(, ).ToLower() == "d")
{
var dir = item.Substring(pos).Trim();
if (dir == "." || dir == "..") continue;
var di = new DirectoryItem();
di.BaseUri = new Uri(address);
di.IsDirectory = true;
di.Name = dir;
di.Items = GetDirectoryInformation(di.AbsolutePath, username, password);
returnValue.Add(di);
}
else if (item.Substring(, ).ToLower() == "-")
{
var filename = item.Substring(pos).Trim();
var di = new DirectoryItem();
di.BaseUri = new Uri(address);
di.IsDirectory = false;
di.Name = filename;
di.Items = null;
returnValue.Add(di);
}
}
}
return returnValue;
} /// <summary>
/// 下载文件
/// </summary>
/// <param name="filePath">下载到哪里</param>
/// <param name="outputFilename">下载后的文件名</param>
/// <param name="fileName">服务器上的文件名</param>
/// <param name="ftpServerIP">服务器全路径,注意最后的斜线不可少。如ftp://172.18.1.152:8009/aaa/</param>
/// <param name="ftpUserID">访问的用户名</param>
/// <param name="ftpPassword">访问的密码</param>
/// <returns></returns>
public int DownloadFile(string filePath, string outputFilename, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
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 + "\\" + outputFilename, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = ;
int readCount;
byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, , bufferSize);
while (readCount > )
{
outputStream.Write(buffer, , readCount);
readCount = ftpStream.Read(buffer, , bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return ;
}
catch (Exception ex)
{
// Logging.WriteError(ex.Message + ex.StackTrace);
System.Windows.Forms.MessageBox.Show(ex.Message);
return -;
}
}

get_directory_file_download

ftp_get_file_and_directory的更多相关文章

随机推荐

  1. java 参数传值

    基本数据类型参数的传值,参数为基本数据类型 class Computer{ int add(int x,int y){ return x+y; } } public class Example4_6 ...

  2. linux下安装rabbitmq

    1.安装erlang虚拟机 Rabbitmq基于erlang语言开发,所有需要安装erlang虚拟机.安装erlang有两种方式: 第一种:使用yum安装: wget -O /etc/yum.repo ...

  3. C++:new 和 delete

    在 C++ 中 , 使用 new 操作符动态申请内存的时候,如果申请失败,则会抛出  bad_alloc异常 当使用  delete 释放一块内存的时候 , 有些编译器上delete 不能判断一块内存 ...

  4. postfix疯狂外发垃圾邮件

    分析 一.查找main.cf配置文件 localhost# find / -name main.cf /etc/postfix/main.cf 二.打开/etc/postfix/main.cf来看看. ...

  5. linux proxy

    ALL_PROXY=socks://192.168.2.1:3128/ HTTPS_PROXY=https://192.168.2.1:3128/HTTP_PROXY=http://192.168.2 ...

  6. myeclipse中打开java文件中文乱码

    中文乱码肯定是编码与解码不一样导致. 1.如果是平时写代码都没有问题,但是打开其他项目时出现这种问题: window->preferences->General->Content T ...

  7. java日期格式大全 format SimpleDateFormat(转)

    java日期格式大全 format SimpleDateFormat   /**    * 字符串转换为java.util.Date<br>    * 支持格式为 yyyy.MM.dd G ...

  8. vim的复制粘贴小结(转)

    原文地址:http://lsong17.spaces.live.com/blog/cns!556C21919D77FB59!603.entry 内容: 用vim这么久 了,始终也不知道怎么在vim中使 ...

  9. mysql 本地操作

    先把数据库传到根目录 再直接导入  没有50M限制root@b101 [/home/user/www]# mysql -uusername_007li -ppasswd -D dbname_li12d ...

  10. java socket 多线程网络传输多个文件

    http://blog.csdn.net/njchenyi/article/details/9072845 java socket 多线程网络传输多个文件 2013-06-10 21:26 3596人 ...