# # 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)
|