打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ruby版本的ftp批量下载

#
# To change this template, choose Tools | Templates
# and open the template in the editor.
 
require 'net/ftp'


#include Tests

def upload_file(host,user,password,local_folder,remote_folder,size)
  ftp_client=Net::FTP.new(host)
  ftp_client.login(user,password)
  get_folder("*",remote_folder,ftp_client,create_folder(local_folder),size)
  ftp_client.close
end

#
def create_folder(local_folder)
  folder=local_folder+'\\'+Time.now.strftime("%y%m%d")
  unless File.exist?(folder)
    Dir.mkdir(folder, 0)
  end
  folder
end

def get_folder(file_mark,path,ftp_client,folder,size)
  #dirs=ftp_client.dir path
  dirs=ftp_client.ls path
  ftp_client.chdir(path)
  dirs.each { |dir|
    infos=dir.split(' ')
    info=infos.last.sub("http://zsbfree.blog.163.com/blog/static/23400688200811315157711/file://n/","")
    if dir[0,1]=="d"
      unless info=='.'||info=='..'
        folder_file=folder+"http://zsbfree.blog.163.com/blog/static/23400688200811315157711/file://%22+info/
        unless File.exist?(folder_file)
          Dir.mkdir(folder_file,1)
        end
       
        get_folder(file_mark,path+"/"+info,ftp_client,folder_file,size)
        ftp_client.chdir(path)
      end
    elsif dir[0,2]=="-r"
      file=folder+"http://zsbfree.blog.163.com/blog/static/23400688200811315157711/file://%22+info/
      if(File.exist?(file))
        remoting_size=ftp_client.size(info)
        local_size=File.size(file)
       
        if local_size<remoting_size
          ftp_client.sendcmd("REST "+local_size.to_s)
          open(flie,'ab') {|f|
            ftp_client.getbinaryfile(info, file,size) { |i|
                f<<i
             }
          }
        elsif local_size>remoting_size
          getfile(info,file,ftp_client,size)
        end
      else
       get_file(info,file,ftp_client,size)
      end
    end
  }
end

def get_file(info,file,ftp_client,size)
   open(file,'wb') {|f|
     ftp_client.getbinaryfile(info, file,size) { |i|
       f<<i
       }
    }
    puts file+" has download"
end


#puts Test.new(super_class)
upload_file('219.234.89.155','tianc','zhuzhu','c:\\temp','/temp',1024)


 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Django简单实现创建,更新,删除的功能,外加图片的简单处理
用python简单查找大文件
PHP文件系统
文件上传
Python3批量转换文本文件编码
php页面缓存实现方法总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服