由下载地址得到下载包的精确大小

http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html#method-i-head
 
head(path, initheader = nil)click to toggle source

Gets only the header from path on the connected-to host. header is a Hash like { ‘Accept’ => ‘/’, … }.

This method returns a Net::HTTPResponse object.

This method never raises an exception.

response = nil
Net::HTTP.start('some.www.server', 80) {|http|
  response = http.head('/index.html')
}
p response['content-type']


于是就有了这样的奇葩方法,欢迎拍砖

  def self.get_the_package_size origin_url
    if origin_url.present?
      if origin_url.index(".com")
        array = origin_url.split(".com").reverse
        url_end = array[0]
        url_begin = array[1].split("//")[1]
        url_begin = "#{url_begin}.com"
      elsif origin_url.index(".net")
        array = origin_url.split(".net").reverse
        url_end = array[0]
        url_begin = array[1].split("//")[1]
        url_begin = "#{url_begin}.net"
      else
        return ""
      end 
      response = nil 
      Net::HTTP.start(url_begin, 80, "10.103.11.54", 81) {|http|
        response = http.head(url_end)
      }   
      return response['Content-Length']
    end 
  end 


其实还可以直接用命令看一下哦
curl -v -XHEAD http://dl.g.youku.com/20150317/1426557582_XXXX-20150312.apk

原文地址:https://www.cnblogs.com/iwangzheng/p/4345534.html