6

使用以下ftp_download方法有效,但如果我改变

ftp.getbinaryfile(file,localdir,1024)   #=> Saves the file to localdir

ftp.getbinaryfile(file)    #=> returns nil

我得到nil回报。根据

http://www.ruby-doc.org/stdlib-2.0/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-getbinaryfile

inilf 我设置localfilenil如上,数据应该由方法检索和返回。我究竟做错了什么?

def ftp_download(domain,remotedir,filename_regex,user=nil,pwd=nil)
  ftp = Net::FTP::new(domain)
  if user && pwd
    ftp.login(user, pwd)
  end
  ftp.chdir(remotedir)
  fileList = ftp.nlst(filename_regex)

  fileList.each do |file|
    localdir=File.join(remotedir,file)
    localdir=localdir[1..-1] if localdir[0]="/"
    FileUtils.mkdir_p(File.dirname(localdir))
    ftp.getbinaryfile(file,localdir,1024)
  end
  ftp.close
end
4

1 回答 1

11

If you look at the getbinaryfile method signature you will notice that the default value for the second parameter (localfile) is not nil but File.basename(remotefile)

getbinaryfile(remotefile, 
              localfile=File.basename(remotefile), 
              blocksize=DEFAULT_BLOCKSIZE)

If you want localfile to be nil you have to pass it explicitly:

ftp.getbinaryfile(file, nil)
于 2013-05-08T14:47:14.077 回答