【问题标题】:How can I use Net::Http to download a file with UTF-8 characters in it?如何使用 Net::Http 下载包含 UTF-8 字符的文件?
【发布时间】:2017-08-25 22:58:48
【问题描述】:

我有一个应用程序,用户可以在其中上传持久保存到 S3 的基于文本的文件(xml、csv、txt)。其中一些文件非常大。需要对这些文件中的数据执行各种操作,因此我没有从 S3 读取它们并偶尔超时,而是将文件下载到本地,然后对它们进行松散的操作。

这是我用来从 S3 下载文件的代码。 Upload 是我用来存储此信息的 AR 模型的名称。该方法是 Upload 模型上的实例方法:

def download
  basename = File.basename(self.text_file_name.path)
  filename = Rails.root.join(basename)
  host = MyFitment::Utility.get_host_without_www(self.text_file_name.url)
  Net::HTTP.start(host) do |http|
    f = open(filename)
    begin
      http.request_get(self.text_file_name.url) do |resp|
        resp.read_body do |segment|
          f.write(segment) # Fails when non-ASCII 8-bit characters are included.
        end
      end
    ensure
      f.close()
    end
  end
  filename

end

所以你会看到上面加载失败的那一行。此代码以某种方式认为所有下载的文件都以 ASCII 8 位编码。我该怎么做:

1) 像这样检查远程文件的编码 2) 下载并写入成功。

这是当前特定文件发生的错误:

Encoding::UndefinedConversionError: "\x95" from ASCII-8BIT to UTF-8
from /Users/me/code/myapp/app/models/upload.rb:47:in `write'

感谢您提供的任何帮助!

【问题讨论】:

    标签: ruby encoding amazon-s3 character-encoding net-http


    【解决方案1】:

    我该怎么做:1)检查远程文件的编码。

    您可以检查响应的 Content-Type 标头,如果存在,它可能看起来像这样:

    Content-Type: text/plain; charset=utf-8
    

    如您所见,编码是在那里指定的。如果没有 Content-Type 标头,或者没有指定 charset,或者 charset 指定不正确,那么您将无法知道文本的编码。有些宝石可以尝试猜测编码(提高准确性),例如rchardetcharlock_holmes,但是为了完全准确,你必须在阅读文本之前知道编码。

    此代码以某种方式认为所有下载的文件都以 ASCII 8 位。

    在 ruby​​ 中,ASCII-8BIT 等价于 binary,这意味着 Net::HTTP 库只为您提供一个包含一系列单个字节的字符串,由您决定如何解释这些字节。

    如果您想将这些字节解释为 UTF-8,那么您可以使用 String#force_encoding()

    text = text.force_encoding("UTF-8")
    

    例如,如果您想对字符串进行一些正则表达式匹配,并且想要匹配完整字符(可能是多字节)而不仅仅是单个字节,您可能想要这样做。

    Encoding::UndefinedConversionError: "\x95" from ASCII-8BIT to UTF-8

    使用String#encode('UTF-8') 将 ASCII-8BIT 转换为 UTF-8 不适用于 ascii 码大于 127 的字节:

    (0..255).each do |ascii_code|
      str = ascii_code.chr("ASCII-8BIT")
      #puts str.encoding   #=>ASCII-8BIT
    
      begin
        str.encode("UTF-8")
      rescue Encoding::UndefinedConversionError
        puts "Can't encode char with ascii code #{ascii_code} to UTF-8."
      end
    
    end
    
    --output:--
    Can't encode char with ascii code 128 to UTF-8.
    Can't encode char with ascii code 129 to UTF-8.
    Can't encode char with ascii code 130 to UTF-8.
    ...
    ...
    Can't encode char with ascii code 253 to UTF-8.
    Can't encode char with ascii code 254 to UTF-8.
    Can't encode char with ascii code 255 to UTF-8.
    

    Ruby 每次只从 ASCII-8BIT 字符串中读取一个字节,并尝试将字节中的字符转换为 UTF-8。因此,虽然 128 作为多字节字符序列的一部分可能是 UTF-8 中的合法字节,但作为单字节,128 不是合法的 UTF-8 字符。

    至于将字符串写入文件,而不是这样:

    f = open(filename)
    

    ...如果你想将 UTF-8 输出到文件中,你会写:

    f = open(filename, "w:UTF-8")
    

    默认情况下,ruby 使用Encoding.default_external 的任何值将输出编码到文件中。 default_external 编码是从您的系统环境中提取的,或者您可以显式设置它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 2017-09-13
      • 2012-02-06
      • 2016-04-24
      • 2018-09-21
      • 2013-04-23
      • 1970-01-01
      相关资源
      最近更新 更多