【问题标题】:Images uploaded with Via Perl Net::FTP get corrupted通过 Perl Net::FTP 上传的图像损坏
【发布时间】:2014-11-21 02:46:26
【问题描述】:

为什么我在上传到 FTP 服务器时总是收到损坏的图像文件? .gif 图像不会损坏,只有 .jpeg/jpg.png 会损坏。

sub png{
    my $ftp=Net::FTP->new($fhost)or die &ftpErr;
    $ftp->login($hostname, $hostpass);
    my $img=$ftp->put("$file");
    $ftp->get($img);
    $ftp->quit;
    our $image="$img";
    our $shot=$window->Photo(-format=>'png',-file=>"$image");
    $window->Label(-relief=>'ridge',-image=>$shot,-width=>50,-height=>50)->pack(-anchor=>'n');
}
sub jpeg{
    my $ftp=Net::FTP->new($fhost)or die &ftpErr;
    $ftp->login($hostname, $hostpass);
    my $img=$ftp->put("$file");
    $ftp->get($img);
    $ftp->quit;
    our $image="$img";
    our $shot=$window->Photo(-format=>'jpeg',-file=>"$image");
    $window->Label(-relief=>'ridge',-image=>$shot,-width=>50,-height=>50)->pack(-anchor=>'n');
}

【问题讨论】:

    标签: perl sftp net-ftp


    【解决方案1】:

    您正在以默认模式传输文件,即 ASCII。此模式翻译行尾。传输二进制文件使用二进制模式:

      $ftp->binary;
      $ftp->put(...);
      $ftp->get(...);
    

    【讨论】:

    • 确保$ftp->binary在登录后
    • @CJ7: 二进制模式应该在上传/下载之前设置。其实每次上传/下载都可以设置不同的。
    • 但需要在用户名和密码步骤之后。
    • @CJ7: Net::FTP 没有自己设置类型。某些 FTP 服务器可能会重置登录前所做的任何类型设置。为了保存,最好在开始传输之前立即设置类型。
    • @CJ17 谢谢兄弟。创建对象后,我打开 binmode。这很麻烦,因为我需要在登录后立即进行。
    猜你喜欢
    • 1970-01-01
    • 2011-06-22
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多