FTP 批量上传

现在都不怎么做开发了,基本上想要用到什么就弄什么,由此弄了许多小工具。

 

还有 QQ 邮件群发、FTP 密码破解、百度关键字站点抓取……大部分都是 HACK 用的 :)

 

之前弄 Shell 工具,想要批量上传一些文件,就弄了这个工具。

 

程序目录下想要建立 upfile 文件夹,里面放置想要上传的文件,不支持子目录。

 

上传成功的写入 log_yyyMMddhhmmss.txt 文件中。

 

要批量上传的 FTP 账号放置到程序同一目录下的 ftp.txt 文件中。

 

ftp.txt 的格式(每行一个账号):

aaa.com,用户,密码

bbb.com,用户,密码

 

点此下载源码

 

源码展开可见
     class  Program
    {
        
const   int  TIMEOUT  =   10000 ;
        
const   int  MAXREAD  =   2048 ;

        
static   void  UpLoadFile(FtpWebRequest ftp,  string  path)
        {
            ftp.Method 
=  WebRequestMethods.Ftp.UploadFile;
            var buffer 
=   new   byte [MAXREAD];
            
using  (var fs  =   new  FileStream(path, FileMode.Open))
            {
                ftp.ContentLength 
=  fs.Length;
                
using  (var sw  =  ftp.GetRequestStream())
                {
                    var read 
=  fs.Read(buffer,  0 , MAXREAD);
                    
while  (read  !=   0 )
                    {
                        sw.Write(buffer, 
0 , read);
                        read 
=  fs.Read(buffer,  0 , MAXREAD);
                    }
                }
            }
        }

        
static  FtpWebRequest FtpCreate( string  url,  string  userName,  string  passWord)
        {
            var ftp 
=  (FtpWebRequest)FtpWebRequest.Create(url);
            ftp.Credentials 
=   new  NetworkCredential(userName, passWord);
            ftp.UseBinary 
=   true ;
            ftp.Timeout 
=  TIMEOUT;
            ftp.KeepAlive 
=   false ;
            
return  ftp;
        }

        
static   string [] GetDirectory( string  url,  string  userName,  string  passWord)
        {
            
try
            {
                var ftp 
=  FtpCreate(url, userName, passWord);
                ftp.Method 
=  WebRequestMethods.Ftp.ListDirectory;
                
using  (var sw  =  ftp.GetResponse().GetResponseStream())
                {
                    
using  (var sr  =   new  StreamReader(sw, Encoding.Default))
                        
return  sr.ReadToEnd().Replace( " \r " "" ).Split( ' \n ' );
                }
            }
            
catch  {  return   null ; }
        }

        
static   void  Main( string [] args)
        {
            var index 
=   0 ;
            var now 
=  DateTime.Now;
            Console.ForegroundColor 
=  ConsoleColor.Cyan;
            Console.WriteLine(
" log_{0:yyyMMddhhmmss}.txt " , now);
            
try
            {
                var files 
=   new  DirectoryInfo( " upfile " ).GetFiles( " *.* " , SearchOption.TopDirectoryOnly);
                
using  (var sr  =   new  StreamReader( " ftp.txt " , Encoding.Default,  true ))
                {
                    
using  (var log  =   new  StreamWriter( string .Format( " log_{0:yyyMMddhhmmss}.txt " , now),  false ))
                    {
                        
using  (var error  =   new  StreamWriter( " error.txt " false ))
                        {
                            var s 
=   "" ;
                            
while  ((s  =  sr.ReadLine())  !=   null )
                            {
                                index
++ ;
                                Console.ForegroundColor 
=  ConsoleColor.Yellow;
                                Console.Write((
" NO: "   +  index.ToString()).PadRight( 8 ));
                                var arr 
=  s.Split( ' , ' );
                                var url 
=   " ftp:// "   +  arr[ 0 +   " / " ;
                                Console.ResetColor();
                                Console.Write(url);
                                var dir 
=  GetDirectory(url, arr[ 1 ], arr[ 2 ]);
                                
if  (dir  !=   null )
                                {
                                    
if  (Array.Exists(dir,  delegate ( string  o) {  return   string .Equals(o,  " wwwroot " , StringComparison.OrdinalIgnoreCase); }))
                                        url 
+=   " wwwroot/ " ;
                                    
else   if  (Array.Exists(dir,  delegate ( string  o) {  return   string .Equals(o,  " web " , StringComparison.OrdinalIgnoreCase); }))
                                        url 
+=   " web/ " ;
                                    
else   if  (Array.Exists(dir,  delegate ( string  o) {  return   string .Equals(o,  " www " , StringComparison.OrdinalIgnoreCase); }))
                                        url 
+=   " www/ " ;
                                }
                                
try
                                {
                                    
foreach  (var file  in  files)
                                    {
                                        var ftp 
=  FtpCreate(url  +  file.Name, arr[ 1 ], arr[ 2 ]);
                                        UpLoadFile(ftp, file.FullName);
                                        Console.ResetColor();
                                        Console.Write(
" \r\n...  " .PadRight( 10 +  url  +  file.Name);
                                        Console.ForegroundColor 
=  ConsoleColor.Green;
                                        Console.Write(
"   OK! " );
                                    }
                                    Console.WriteLine();
                                    url 
=   " http:// "   +  arr[ 0 +   " / " ;
                                    log.WriteLine(url);
                                    log.Flush();
                                }
                                
catch  (Exception e)
                                {
                                    error.WriteLine(
string .Join( " , " , arr));
                                    error.Flush();
                                    Console.ForegroundColor 
=  ConsoleColor.Red;
                                    Console.WriteLine(
"    "   +  e.Message);
                                }
                            }
                        }
                    }
                }
            }
            
catch  (Exception e)
            {
                Console.ForegroundColor 
=  ConsoleColor.Red;
                Console.WriteLine(e.Message);
            }
            Console.ResetColor();
            Console.WriteLine(
" *************************************************************** " );
            Console.ForegroundColor 
=  ConsoleColor.Cyan;
            Console.WriteLine(
" End! " );
            Console.Read();
        }
    }

 

 

你可能感兴趣的:(ftp)