c#上传下载ftp(支持断点续传)

这个ftpClient是从网上找来的,自己加了断点续传的方法

usingSystem;
usingSystem.Net;
usingSystem.IO;
usingSystem.Text;
usingSystem.Net.Sockets;

namespaceftpGet
{
///<summary>
///FTPClient
///</summary>

publicclassFTPClient
{
构造函数

登陆字段、属性

链接

传输模式

文件操作

上传和下载

目录操作

内部变量

内部函数
}

}



当然,大家还要看看Main方法
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.IO;

namespaceftpGet
{
classProgram
{
staticstringremotingFolder=System.Configuration.ConfigurationSettings.AppSettings["remotingFolder"];//远程ftp文件目录
staticstringlocalFolder=System.Configuration.ConfigurationSettings.AppSettings["localFolder"];//要下载到的本地目录
staticstringftpServer=System.Configuration.ConfigurationSettings.AppSettings["ftpServer"];//ftp服务器
staticstringuser=System.Configuration.ConfigurationSettings.AppSettings["user"];//用户名
staticstringpwd=System.Configuration.ConfigurationSettings.AppSettings["pwd"];//密码
staticstringport=System.Configuration.ConfigurationSettings.AppSettings["port"];//端口
staticvoidMain(string[]args)
{
FTPClientclient=newFTPClient(ftpServer,"/",user,pwd,int.Parse(port));
client.Connect();
GetFolder("*",remotingFolder,client,CreateFolder());
client.DisConnect();
ClearFolder();
Console.WriteLine("下载完毕");
System.Threading.Thread.Sleep(3000);
}


///<summary>
///在本地目录下创建一个以日期为名称的目录,我做这个ftp的主要目的是为了每天都备份
///</summary>
///<returns>创建的目录名</returns>

privatestaticstringCreateFolder()
{
stringfolder=localFolder+"\\"+DateTime.Now.ToShortDateString();
if(!Directory.Exists(folder))
Directory.CreateDirectory(folder);

returnfolder;
}


///<summary>
///在下载结束后清空程序目录的多余文件
///</summary>

privatestaticvoidClearFolder()
{
stringfolder=Environment.CurrentDirectory;
string[]dictorys=Directory.GetFiles(folder);
foreach(stringdictoryindictorys)
{
FileInfoinfo=newFileInfo(dictory);
if(info.Length==0)
File.Delete(dictory);
}

}


///<summary>
///递归获取ftp文件夹的内容
///</summary>
///<paramname="fileMark">文件标记</param>
///<paramname="path">远程路径</param>
///<paramname="client"></param>
///<paramname="folder"></param>

privatestaticvoidGetFolder(stringfileMark,stringpath,FTPClientclient,stringfolder)
{
string[]dirs=client.Dir(path);//获取目录下的内容
client.ChDir(path);//改变目录
foreach(stringdirindirs)
{
string[]infos=dir.Split('');
stringinfo=infos[infos.Length-1].Replace("\r","");
if(dir.StartsWith("d")&&!string.IsNullOrEmpty(dir))//为目录
{

if(!info.EndsWith(".")&&!info.EndsWith(".."))//筛选出真实的目录
{
Directory.CreateDirectory(folder+"\\"+info);
GetFolder(fileMark,path+"/"+info,client,folder+"\\"+info);
client.ChDir(path);
}

}

elseif(dir.StartsWith("-r"))//为文件
{
stringfile=folder+"\\"+info;
if(File.Exists(file))
{
longremotingSize=client.GetFileSize(info);
FileInfofileInfo=newFileInfo(file);
longlocalSize=fileInfo.Length;

if(remotingSize!=localSize)//短点续传
{
client.GetBrokenFile(info,folder,info,localSize);
}

}

else
{
client.GetFile(info,folder,info);//下载文件
Console.WriteLine("文件"+folder+info+"已经下载");
}

}

}


}

}

}


配置文件
<? xmlversion="1.0"encoding="utf-8" ?>
< configuration >
< appSettings >
< add key ="remotingFolder" value ="/temp" />
< add key ="localFolder" value ="c:\temp" />
< add key ="ftpServer" value ="*" />
< add key ="user" value ="*" />
< add key ="pwd" value ="*" />
< add key ="port" value ="21" />
</ appSettings >
</ configuration >

你可能感兴趣的:(断点续传)