ftp_get_file_and_directory
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的更多相关文章
随机推荐
- UVALive 6887 Book Club
最大流,有向环覆盖问题. #include<cstdio> #include<cstring> #include<string> #include<cmath ...
- 关于js的一些基本知识(类,闭包,变量)
这里写的都是些杂知识,包括私有,类,闭包这些js不可避免的东西,感觉自己有可能会误人子弟.所以有觉得写错了的读者,希望可以及时评论告诉我.我可以及时更正.多谢大家了 1.关于类的创建 类的创建大致可以 ...
- spider JAVA如何判断网页编码 (转载)
原文链接 http://www.cnblogs.com/nanxin/archive/2013/03/27/2984320.html 前言 最近做一个搜索项目,需要爬取很多网站获取需要的信息.在爬取网 ...
- 安装tomcat过程中出现问题小结
报错信息如下:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these ...
- 通过ant调用shell脚本执行adb命令
在Hudson或者Jenkins中利用ant的exec 来调用shell命令,通过shell脚本来执行adb shell命令,可以正常执行,不会出现在ant中直接调用adb shell出现的假死情况. ...
- Tomcat安装与配置
Tomcat概述Tomcat是Apache 软件基 金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同 ...
- 读 《我为什么放弃Go语言》 有感
最近又熟悉了下go语言,发现go语言还有许多设计不好的地方,然后又读到了<我为什么放弃Go语言>这篇文章, 对于某些方面,我还是比较认同的. 这篇文章总结了十六点,如下: 1.1 不允许左 ...
- 在线生成二叉树(基于EaselJS(canvas))
学习二叉树的时候,老在本子上画二叉树好麻烦.其实就想看下树结构.最近html5蛮火的,就用canvas和EaselJS.js(开发flash公司开发的插件)插件实现了个.大家随便用吧. 这是个什么东西 ...
- js中焦点的含义是什么
焦点即是光标 焦点是在页面上屏幕中闪动的的小竖线,鼠标点击可获得光标,Tab键可按照设置的Tabindex切换焦点
- winutils spark windows installation
http://stackoverflow.com/questions/37305001/winutils-spark-windows-installation