如何在下载文件之前获取文件大小 LWP::useragent

如何在下载文件之前获取文件大小 LWP::useragent

我写了一个脚本,这样我就可以使用自动从热文件下载LWP::用户代理。我已成功让文件正确下载。如何在下载前获取文件大小?

我需要这个来显示文件的大部分内容已下载。

答案1

下面是实现此操作的简短代码片段。

use LWP::UserAgent;

sub GetFileSize{
    my $url=shift;
    $ua = new LWP::UserAgent;
    $ua->agent("Mozilla/5.0");
    my $req = new HTTP::Request 'HEAD' => $url;
    $req->header('Accept' => 'text/html');
    $res = $ua->request($req);
    if ($res->is_success) {
        my $headers = $res->headers;
        return $headers;
    }
    return 0;
}

$link = 'http://www.domain.com/anyfile.zip';
$header = GetFileSize($link);

print "File size: " . $header->content_length . " bytes\n";
print "Last moified: " . localtime($header->last_modified) . "\n";
exit;

来源

答案2

获取尺寸非常简单:

use LWP::Simple;

my $url='http://www.superuser.com/favicon.ico';
my ($type, $size) = head($url) or die "ERROR $url: $!";
print "$url: type: $type, size: $size$/";

然而,对于实际进度指标您必须注册一个回调,如下所示:

use LWP::UserAgent;

my $url='http://superuser.com/questions/200468/how-to-get-file-size-before-downloading-the-file-lwpuseragent?rq=1';#'http://www.superuser.com/favicon.ico';
my $ttlDown = 0;
my $resp = LWP::UserAgent->new()->get($url, ':content_cb' => sub {
      my ($data, $response) = @_;
      my $size = $response->content_length;
      $ttlDown += length $data;
      printf("%7.1f KB of %7.1f (%5.1f%%)$/", 
        $ttlDown / 1024.0, $size / 1024.0, $ttlDown * 100.0 / $size
      );
      ##### TODO: append $data to file #####
});
print "$/-----$/".$resp->as_string();

注意这一##### TODO行:您可能想要将接收到的字节写入磁盘。

答案3

以上答案来自尼夫尔对我帮助很大。我只想补充一点,对于经过身份验证的 HTTP 站点,您只需添加一行,$req->authorization_basic( “$name”, “$pwd” ) ;而且它运行良好。

sub GetFileSize{
    my $url=shift;
    my $name = shift;
    my $pwd = shift;

    my $browser = LWP::UserAgent->new;
    $browser->agent("Mozilla/5.0");
    my $req =  HTTP::Request->new( HEAD => "$url");
    $req->authorization_basic( "$name", "$pwd" );

    $req->header('Accept' => 'text/html');
    my $res = $browser->request($req);
    if ($res->is_success) {
        my $headers = $res->headers;
        return $headers->content_length;
    }
    return 0;
}

相关内容