Perl Net::FTP::递归:不能下载文件夹,只能下载子文件夹的文件



我成功地从ftp服务器的子文件夹下载了文件。但如果我想从上层下载文件夹,它是不起作用的。

以下是文件夹结构:

  • 文件夹rwx-r-x
    • 子文件夹1 rwx r-x r-x
      • 文件1 rw-r--r--
      • 文件2 rw-r--r--
    • 子文件夹2 rxx r-x r-x
      • 文件3 rw-r--r--
      • 文件4 rw-r--r--

如果我使用这个:

$f1->cwd("/folder/subfolder1");
$f1->rget();
$f1->quit;

将下载文件file1和file2。

如果我使用这个:

$f1->cwd("/folder");
$f1->rget();
$f1->quit;

由于超时,将不会下载任何内容,并且程序已完成。我预计它会下载子文件夹1和子文件夹2以及子文件夹的内容。对此有什么解释吗?我如何通过下载子文件夹和文件的方式解决它?

代码的详细描述在这里

更新1:调试

调试

my $f1 = Net::FTP::Recursive->new($host1, Debug => 1) or die "Can't open $host1n";

给出以下内容:

Net::FTP::Recursive=GLOB(0x312bf50)>>> CWD /folder
Net::FTP::Recursive=GLOB(0x312bf50)<<< 250 CWD command successful
Net::FTP::Recursive=GLOB(0x312bf50)>>> PWD
Net::FTP::Recursive=GLOB(0x312bf50)<<< 257 "/folder" is the current directory
Net::FTP::Recursive=GLOB(0x312bf50)>>> PASV
Net::FTP::Recursive=GLOB(0x312bf50)<<< 227 Entering Passive Mode (188,40,220,103,255,187).
Net::FTP::Recursive=GLOB(0x312bf50)>>> LIST
Net::FTP::Recursive=GLOB(0x312bf50)<<< 150 Opening BINARY mode data connection for file list
Timeout at C:/Strawberry/perl/lib/Net/FTP.pm line 1107.

更新2:在C/Strawberry/perl/lib/Net/FTP.pm行1107处超时。

_list_cmd是调试输出中提到的行的函数。我还添加了使用_list_cmd的行和换行,以使其更易于阅读,从而保留行号。

671 # Try to delete the contents
672 # Get a list of all the files in the directory, excluding 
#     the current and parent directories 
673 my @filelist = map { /^(?:S+;)+ (.+)$/ ? ($1) : () } 
grep { !/^(?:S+;)*type=[cp]dir;/i } $ftp->_list_cmd("MLSD", $dir);
925 sub ls  { shift->_list_cmd("NLST", @_); }
925 sub dir { shift->_list_cmd("LIST", @_); }

1087 sub _list_cmd {
1088   my $ftp = shift;
1089   my $cmd = uc shift;
1090 
1091   delete ${*$ftp}{'net_ftp_port'};
1092   delete ${*$ftp}{'net_ftp_pasv'};
1093 
1094   my $data = $ftp->_data_cmd($cmd, @_);
1095 
1096   return
1097     unless (defined $data);
1098 
1099   require Net::FTP::A;
1100   bless $data, "Net::FTP::A";    # Force ASCII mode
1101 
1102   my $databuf = '';
1103   my $buf     = '';
1104   my $blksize = ${*$ftp}{'net_ftp_blksize'};
1105 
1106   while ($data->read($databuf, $blksize)) {
1107     $buf .= $databuf;
1108   }
1109 
1110   my $list = [split(/n/, $buf)];
1111 
1112   $data->close();
1114   if (EBCDIC) {
1115     for (@$list) { $_ = $ftp->toebcdic($_) }
1116   }
1117 
1118   wantarray
1119     ? @{$list}
1120     : $list;
1121 }

要下载目录和文件,a尝试了以下解决方法:在所有子目录上使用循环并应用rget。它完成了我想要的工作。然而,rget在上层不起作用的原因仍然没有得到回答。至少现在很清楚,这不是一个权限问题。

# ftp-server directory
my $ftpdir = "folder";
# Defie local download folder
my $download = "C:/local";
chdir($download);       
# Change to remote directory
$f1->cwd($ftpdir) or die "Can't cwd to $ftpdirn", $f1->message;
# grep all folder of top level
my @ftp_directories = $f1->ls;
# remove . and ..
@ftp_subdir = grep ! /^.+$/, @ftp_subdir;
foreach my $sd (@ftp_subdir) {
# Make folder on local computer
my $localdir = catfile($download,$sd);
mkdir $localdir;
# Change local working directory
chdir $localdir;

# Change to remote sub directory to be downloaded
$f1->cwd($sd) or die "Can't cwd to $sdn";
}
# download files
$f1->rget();
# Change to upper level
$f1->cwd("..");
}  
$f1->quit;

最新更新