【问题标题】:How can I check the size of a file over FTP using Perl?如何使用 Perl 通过 FTP 检查文件的大小?
【发布时间】:2009-11-18 07:28:01
【问题描述】:

我有 FTP Perl 脚本,我想通过检查传输到远程服务器的字节数是否等于本地服务器中文件的实际字节数来确定文件传输是否完成。我怎么能做到这一点?

这是我目前所拥有的:

 my $ftp = Net::FTP->new($host, Debug => 1)
 or die "Could not connect to '$host': $@";

 $ftp->login($user, $pw)
 or die sprintf "Could not login: %s", $ftp->message;

 $ftp->cwd($path)
 or die sprintf "Could not login: %s", $ftp->message;

 $ftp->ls;

 $ftp->binary;

 $ftp->get($file)
 or die sprintf "Could not login: %s", $ftp->message;

【问题讨论】:

    标签: perl ftp


    【解决方案1】:

    从文档中,您可以使用 size()

    大小(文件) 返回存储在远程服务器上的给定文件的大小(以字节为单位)。 注意:报告的大小是远程存储文件的大小 服务器。如果文件随后以 ASCII 格式从服务器传输 模式和远程服务器和本地机器有不同的想法 "End Of Line" 然后是传输后本地机器上的文件大小 可能不同。

    代码:

    my $host="127.0.0.1";
    my $user="anonymous";
    my $pw = "asdfsf";
    my $path="pub";
    my $file="file";
    my $ftp = Net::FTP->new($host, Debug => 0) 
        or die "Could not connect to '$host': $@";
    
    $ftp->login($user, $pw) or die sprintf "Could not login: %s", $ftp->message;
    $ftp->cwd($path) or die sprintf "Could not login: %s", $ftp->message;
    $ftp->binary;
    print $ftp->size($file) or die sprintf "Could not login: %s", $ftp->message;
    $ftp->quit();
    

    【讨论】:

    • 这很好,但我想比较传输前本地文件的大小和传输后远程文件的大小。我该怎么办?
    【解决方案2】:
    print "FTP size = ", $ftp->size($file), "\n";
    print "Local size = ", (-s $file), "\n";
    

    【讨论】:

    • 如果不将 ftp 设置为二进制模式,这将无法工作:$ftp->binary;
    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2014-11-20
    • 2018-04-07
    • 1970-01-01
    相关资源
    最近更新 更多