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

http://www.cnblogs.com/zsbfree/archive/2008/01/29/1057643.html

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

None.gif using  System;
None.gif using  System.Net;
None.gif using  System.IO;
None.gif using  System.Text;
None.gif using  System.Net.Sockets;
None.gif
None.gif namespace  ftpGet
ExpandedBlockStart.gif {
ExpandedSubBlockStart.gif    /// <summary>
InBlock.gif    /// FTP Client
ExpandedSubBlockEnd.gif    /// </summary>

InBlock.gif    public class FTPClient
ExpandedSubBlockStart.gif    {
ContractedSubBlock.gif        构造函数
InBlock.gif
ContractedSubBlock.gif        登陆字段、属性
InBlock.gif
ContractedSubBlock.gif        链接
InBlock.gif
ContractedSubBlock.gif        传输模式
InBlock.gif
ContractedSubBlock.gif        文件操作
InBlock.gif
ContractedSubBlock.gif        上传和下载
InBlock.gif
ContractedSubBlock.gif        目录操作
InBlock.gif
ContractedSubBlock.gif        内部变量
InBlock.gif
ContractedSubBlock.gif        内部函数
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


当然,大家还要看看Main方法
None.gif using  System;
None.gif using  System.Collections.Generic;
None.gif using  System.Text;
None.gif using  System.IO;
None.gif
None.gif namespace  ftpGet
ExpandedBlockStart.gif {
InBlock.gif    class Program
ExpandedSubBlockStart.gif    {
InBlock.gif        static string remotingFolder = System.Configuration.ConfigurationSettings.AppSettings["remotingFolder"];  //远程ftp文件目录
InBlock.gif        static string localFolder = System.Configuration.ConfigurationSettings.AppSettings["localFolder"];  //要下载到的本地目录
InBlock.gif        static string ftpServer = System.Configuration.ConfigurationSettings.AppSettings["ftpServer"];  //ftp服务器
InBlock.gif        static string user = System.Configuration.ConfigurationSettings.AppSettings["user"];  //用户名
InBlock.gif        static string pwd = System.Configuration.ConfigurationSettings.AppSettings["pwd"];  //密码
InBlock.gif        static string port = System.Configuration.ConfigurationSettings.AppSettings["port"];  //端口
InBlock.gif        static void Main(string[] args)
ExpandedSubBlockStart.gif        {
InBlock.gif            FTPClient client = new FTPClient(ftpServer, "/", user, pwd, int.Parse(port));
InBlock.gif            client.Connect();
InBlock.gif            GetFolder("*", remotingFolder, client, CreateFolder());
InBlock.gif            client.DisConnect();
InBlock.gif            ClearFolder();
InBlock.gif            Console.WriteLine("下载完毕");
InBlock.gif            System.Threading.Thread.Sleep(3000);
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        private static string CreateFolder()
ExpandedSubBlockStart.gif        {
InBlock.gif            string folder=localFolder + "\\"+DateTime.Now.ToShortDateString();
InBlock.gif            if (!Directory.Exists(folder))
InBlock.gif                Directory.CreateDirectory(folder);
InBlock.gif            
InBlock.gif            return folder;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        private static void ClearFolder()
ExpandedSubBlockStart.gif        {
InBlock.gif            string folder = Environment.CurrentDirectory;
InBlock.gif            string[] dictorys = Directory.GetFiles(folder);
InBlock.gif            foreach (string dictory in dictorys)
ExpandedSubBlockStart.gif            {
InBlock.gif                FileInfo info = new FileInfo(dictory);
InBlock.gif                if (info.Length == 0)
InBlock.gif                    File.Delete(dictory);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif        private static void GetFolder(string fileMark, string path, FTPClient client, string folder)
ExpandedSubBlockStart.gif        {
InBlock.gif            string[] dirs = client.Dir(path);  //获取目录下的内容
InBlock.gif            client.ChDir(path);  //改变目录
InBlock.gif            foreach (string dir in dirs)
ExpandedSubBlockStart.gif            {
InBlock.gif                string[] infos = dir.Split(' ');
InBlock.gif                string info = infos[infos.Length - 1].Replace("\r", "");
InBlock.gif                if (dir.StartsWith("d") && !string.IsNullOrEmpty(dir))  //为目录
ExpandedSubBlockStart.gif                {
InBlock.gif
InBlock.gif                    if (!info.EndsWith(".") && !info.EndsWith(".."))  //筛选出真实的目录
ExpandedSubBlockStart.gif                    {
InBlock.gif                        Directory.CreateDirectory(folder + "\\" + info);
InBlock.gif                        GetFolder(fileMark, path + "/" + info, client, folder + "\\" + info);
InBlock.gif                        client.ChDir(path);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                else if (dir.StartsWith("-r"))  //为文件
ExpandedSubBlockStart.gif                {
InBlock.gif                    string file = folder + "\\" + info;
InBlock.gif                    if (File.Exists(file))  
ExpandedSubBlockStart.gif                    {
InBlock.gif                        long remotingSize = client.GetFileSize(info);
InBlock.gif                        FileInfo fileInfo = new FileInfo(file);
InBlock.gif                        long localSize = fileInfo.Length;
InBlock.gif
InBlock.gif                        if (remotingSize != localSize)  //短点续传
ExpandedSubBlockStart.gif                        {
InBlock.gif                            client.GetBrokenFile(info, folder, info, localSize);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

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

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

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

转载于:https://www.cnblogs.com/hxworm/articles/1984673.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值