【问题标题】:Ruby 1.9 + Net::FTP => Encoding::UndefinedConversionErrorRuby 1.9 + Net::FTP => Encoding::UndefinedConversionError
【发布时间】:2012-04-11 21:54:35
【问题描述】:

我从 Ruby 1.8.7 升级到 1.9.2(和 Rails 3.2.2)并且遇到 Net::FTP#gettextfile 抛出 Encoding::UndefinedConversionError 的问题

我正在尝试下载编码为 utf-8 的 xml 文件。如果我使用 getbinaryfile,下载工作正常,但我想了解为什么 gettextfile 不起作用。

# -*- encoding : utf-8 -*-

# working
ftp = Net::FTP.open(host,user,password)
ftp.passive = true
ftp.getbinaryfile("some.xml","tmp/some.xml")

# not working
ftp = Net::FTP.open(host,user,password)
ftp.passive = true
# raises Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8
ftp.gettextfile("some.xml","tmp/some.xml") 

我知道如果像这样使用 File.open 我可以传递外部和内部编码:

File.open("some.xml", "r:ASCI-8BIT:UTF-8")

但在 Net::FTP 中找不到这样的选项。

我也尝试了 gettextfile 的块版本,但效果不佳,并给出了相同的错误消息。

File.open("some.xml", "w:ASCII-8BIT:UTF-8") do |file|
  ftp.gettextfile("some.xml") { |line| file.write line }
end

有人知道这里出了什么问题吗?

【问题讨论】:

    标签: encoding ruby-1.9.2 net-ftp


    【解决方案1】:

    使用 ftp.getbinaryfile 而不是 ftp.gettextfile 然后它再次工作。 :)

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 2014-06-12
      • 2014-11-21
      • 2015-08-10
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多