代码之家  ›  专栏  ›  技术社区  ›  user3521180

为什么我不能使用perl从FTP下载多个文件到本地目录?

  •  -1
  • user3521180  · 技术社区  · 7 年前

    我已经编写了以下代码来连接到ftp位置并将单个文件下载到本地文件系统。

    #!/usr/local/bin/perl -w
    
    use strict;
    use Net::FTP;
    
    my $hostname = 'mirror.anl.gov';
    my $username = 'anonymous';
    my $password = 'username@domain.com';
    
    # Hardcode the directory and filename to get
    my $home = '/pub';
    my $filename = 'motd';
    
    # Hardcode the local directory
    my $localdir = '/home/boy/';
    
    # Open the connection to the host
    
    my $ftp = Net::FTP->new($hostname)
        or die "Cannot connect to $hostname: $@";     # Construct object
    
    $ftp->login($username, $password)
        or die "Cannot login ", $ftp->message;;       # Log in
    
    $ftp->cwd($home)
        or die "Cannot change working directory ", $ftp->message;# Change 
    +directory
    
    my @filelist=$ftp->ls($home);        
    
    print map { "$_\n"} @filelist;
    
    # Now get the file and leave
    $ftp->get($filename,$localdir.$filename)
        or die "Cannot get $filename: $@"; 
    
    $ftp->quit;
    

    foreach 循环开启 @filelist 如下所示

    foreach(@filelist){    
    print map { "$_\n"} @filelist;
    
        # Now get the file and leave
        $ftp->get($filename,$localdir.$filename)
            or die "Cannot get $filename: $@"; 
    }
    

    但现在我在最后一个 die . 因为我必须得到多个文件,所以现在我删除了 $filename 从我的代码,现在 foreach公司

    foreach(@filelist){    
    print map { "$_\n"} @filelist;
    
    # Now get the file and leave
    $ftp->get($localdir)
        or die "Cannot get $filename: $@"; 
    }
    

    但我又犯了同样的错误。现在我知道了 get 函数用于只下载单个文件和 mget

    can not locate object method 'mget' via package NET::FTP
    

    那我们怎么才能完成我的任务呢?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Borodin    7 年前

    现在我知道了 get 函数用于只下载单个文件和 mget 当我们需要下载多个文件时

    mget公司 RFC 959 ,FTP规范。也无法从 Net::FTP 模块。必须使用 NLST GET 命令,加上 glob 必要时过滤。

    NLST公司

    本命令旨在 进一步自动处理文件。例如,在 “多重获取”功能的实现。

        2
  •  3
  •   choroba    7 年前

    for my $filename (@filelist) {
        $ftp->get($filename, "$localdir/$filename")
            or die "...";
    }