新手问题 Ruby 如何调用 net/ftp 接口实现查看 FTP 服务器上的文件列表

vbgfnd · 2015年03月04日 · 最后由 Sh4d0wBlade 回复于 2016年10月26日 · 3403 次阅读

环境: ftp 服务器上存在/A/B 目录,目录下有个文件夹 ABC,向获取 ftp 服务器上文件夹 ABC 下的文件列表

代码:在 irb 上测试

require 'net/ftp'
ftp = Net::FTP.open("200.200.0.3","test","test")
ftp.voidcmd("CWD /A/B")
ftp.nlst("ABC")-------这里报错

报错内容

irb(main):234:0* ftp.nlst("ABC")
SocketError: getnameinfo: ����������������������
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:1083:in `addr'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:1083:in `block (2levels) in <class:BufferedSocket>'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:383:in `makeport'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:421:in `transfercmd'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:514:in `block (2 levels) in retrlines'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:199:in `with_binary'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:512:in `block in retrlines'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:511:in `retrlines'
        from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:744:in `nlst'
        from (irb):234
        from D:/Sangfor-ATT/Ruby200/bin/irb:12:in `<main>'

不知道哪里出了错误,向各位大牛求教

有人说 nlst 方法里面参数要写全路径,后来我用了 files = ftp.nlst("/PR/SC/SC5.0_R2") 这种方法依然报错 irb(main):007:0* files = ftp.nlst("/PR/SC/SC5.0_R2") SocketError: getnameinfo: ���������������������� from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:1083:in addr' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:1083:inblock (2levels) in class:BufferedSocket' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:383:in makeport' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:421:intransfercmd' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:514:in block (2 levels) in retrlines' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:199:inwith_binary' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:512:in block in retrlines' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/monitor.rb:211:inmon_synchronize' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:511:in retrlines' from D:/Sangfor-ATT/Ruby200/lib/ruby/2.0.0/net/ftp.rb:744:innlst' from (irb):7 from D:/Sangfor-ATT/Ruby200/bin/irb:12:in `

'

补充 ftp 服务器系统信息: UNIX emulated by FileZilla

执行主机是 win7

大部分人都没有在 windows 下开发 ruby 的经验,不太好回答,要不你装个虚拟机用 ubuntu 试试?

@MrPasserby 我们执行主机是 windows 的,这个不能换,不过我可以回家试试,看是代码的问题还是操作系统的问题~ 3q

好歹把乱码表示出来看看说的什么的

用 Linux 跑一下先,避免遇到 Windows 的问题

#5 楼 @gyorou 我也没办法吧这个乱码表示出来,我在控制台看到的是为空的显示。。。。

请问这个 ftp 的 open 函数可以指定端口号吗?怎么没看到指定的端口号?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号