功能很简单,用 ruby 脚本将一个目录内的文件利用 http post 发给服务器端,然后服务器端将接收到的文件保存起来 ruby 脚本
require "net/http"
require "find"
require "rest_client"
file_path = "/home/tiny/log"
file_list = []
file_hash = {}
Find.find(file_path) do |path|
next if File.ftype(path) == "directory"
file_list << path
end
file_list.each do |file|
file_hash[file] = File.new(file, "rb")
end
RestClient.post("http://localhost:4567/uploads", :info => file_hash)
服务器端使用的 Sinatra
get '/' do
"main"
end
post '/uploads' do
Dir.mkdir(File.join(Dir.home, "assets")) if !File.exist?(File.join(Dir.home, "assets"))
puts params[:info].class
puts params[:info].size
puts params[:info]
params[:info].each do |file_name, file|
puts file_name.class
puts file.class
end
end
信息
Hash
2
{"/home/tiny/log/log.txt"=>{:filename=>"log.txt", :type=>"text/plain", :name=>"info[/home/tiny/log/log.txt]", :tempfile=>#<File:/tmp/RackMultipart20130502-21736-1rkyz6s>, :head=>"Content-Disposition: form-data; name=\"info[/home/tiny/log/log.txt]\"; filename=\"log.txt\"\r\nContent-Type: text/plain\r\n"}, "/home/tiny/log/zuijin.txt"=>{:filename=>"zuijin.txt", :type=>"text/plain", :name=>"info[/home/tiny/log/zuijin.txt]", :tempfile=>#<File:/tmp/RackMultipart20130502-21736-16i7d6j>, :head=>"Content-Disposition: form-data; name=\"info[/home/tiny/log/zuijin.txt]\"; filename=\"zuijin.txt\"\r\nContent-Type: text/plain\r\n"}}
!! Unexpected error while processing request: Body yielded non-string value ["/home/tiny/log/log.txt", {:filename=>"log.txt", :type=>"text/plain", :name=>"info[/home/tiny/log/log.txt]", :tempfile=>#<File:/tmp/RackMultipart20130502-22154-1vc7awj>, :head=>"Content-Disposition: form-data; name=\"info[/home/tiny/log/log.txt]\"; filename=\"log.txt\"\r\nContent-Type: text/plain\r\n"}]
127.0.0.1 - - [02/May/2013 12:17:44] "POST /uploads HTTP/1.1" 200 - 0.0354
当取消 params[:info] 循环的时候,
!! Unexpected error while processing request: Body yielded non-string value ["/home/tiny/log/log.txt", {:filename=>"log.txt", :type=>"text/plain", :name=>"info[/home/tiny/log/log.txt]", :tempfile=>#<File:/tmp/RackMultipart20130502-22154-1vc7awj>, :head=>"Content-Disposition: form-data; name=\"info[/home/tiny/log/log.txt]\"; filename=\"log.txt\"\r\nContent-Type: text/plain\r\n"}]
就没有这样的信息了 不知道大家有没有遇到过这样的问题??