Ruby读远程文件流

问题描述 投票:4回答:1

我需要将远程文件保存为云存储服务器,所以我必须将此文件读取到文件流中,我发现这篇文章:Open an IO stream from a local file or url的答案是:

require 'open-uri'
file_contents = open('local-file.txt') { |f| f.read }
web_contents  = open('http://www.stackoverflow.com') {|f| f.read }

web_contents不对。然后我将此操作与自定义本地文件上传进行比较,格式为ASCII-8BIT,格式不相同。所以如何从远程文件中获取正确的流。

ruby ruby-on-rails-3 iostream
1个回答
3
投票

好像对我来说:

require 'open-uri'
web_contents  = open('http://www.stackoverflow.com') {|f| f.read }

out_file = File.expand_path("~/Desktop/out.html")

File.open(out_file, "w") do |f|
  f.puts web_contents
end
© www.soinside.com 2019 - 2024. All rights reserved.