惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - 我想我是青蛙

将golang程序注册为windows服务 CentOS上安装spark standalone mode(转载) 关于听歌这回事 c#读取apk 信息 golang 读取mongob数据写入sqlserver golang 通用Contains方法 golang读取文件信息插入mongodb PetaPoco介绍 白话MongoDB(三)(转载) 白话MongoDB(二)(转载) 白话MongoDB(一) (转载) 给文章加入关键字链接 针对firefox ie6 ie7 ie8的css样式hack (转载) 好久不写日志了,现在开始,好好写了。。 sharepoint 查询calendar recurrence sharepoint之lookup字段 sharepoint获取Audiences 获取exchangeserve的calendar的item sharepoint错误处理
c#上传下载ftp(支持断点续传)
我想我是青蛙 · 2008-01-29 · via 博客园 - 我想我是青蛙

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

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;

namespace ftpGet
{
    
/// <summary>
    
/// FTP Client
    
/// </summary>

    public class FTPClient
    
{
        
构造函数

        
登陆字段、属性

        
链接

        
传输模式

        
文件操作

        
上传和下载

        
目录操作

        
内部变量

        
内部函数
    }

}

当然,大家还要看看Main方法

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ftpGet
{
    
class Program
    
{
        
static string remotingFolder = System.Configuration.ConfigurationSettings.AppSettings["remotingFolder"];  //远程ftp文件目录
        static string localFolder = System.Configuration.ConfigurationSettings.AppSettings["localFolder"];  //要下载到的本地目录
        static string ftpServer = System.Configuration.ConfigurationSettings.AppSettings["ftpServer"];  //ftp服务器
        static string user = System.Configuration.ConfigurationSettings.AppSettings["user"];  //用户名
        static string pwd = System.Configuration.ConfigurationSettings.AppSettings["pwd"];  //密码
        static string port = System.Configuration.ConfigurationSettings.AppSettings["port"];  //端口
        static void Main(string[] args)
        
{
            FTPClient client 
= new FTPClient(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>

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


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

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

        }


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

        private static void GetFolder(string fileMark, string path, FTPClient client, string folder)
        
{
            
string[] dirs = client.Dir(path);  //获取目录下的内容
            client.ChDir(path);  //改变目录
            foreach (string dir in dirs)
            
{
                
string[] infos = dir.Split(' ');
                
string info = 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);
                    }

                }

                
else if (dir.StartsWith("-r"))  //为文件
                {
                    
string file = folder + "\\" + info;
                    
if (File.Exists(file))  
                    
{
                        
long remotingSize = client.GetFileSize(info);
                        FileInfo fileInfo 
= new FileInfo(file);
                        
long localSize = fileInfo.Length;

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

                    }

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

                }

            }


        }

    }

}

配置文件

<?xml version="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>