我成功地从ftp服务器的子文件夹下载了文件。但是,如果我想从更高级别下载文件夹,则无法使用。
这是文件夹结构:
如果我使用这个:
$f1->cwd("/folder/subfolder1");
$f1->rget();
$f1->quit;
文件file1和file2将被下载。
如果我使用这个:
$f1->cwd("/folder");
$f1->rget();
$f1->quit;
什么都不会下载,并且由于超时导致程序结束。我希望它将下载subfolder1和subfolder2以及子文件夹的内容。有什么解释吗?如何以下载子文件夹和文件的方式解决它?
该代码的详细描述为here
更新1:调试
使用
进行调试my $f1 = Net::FTP::Recursive->new($host1, Debug => 1) or die "Can't open $host1\n";
给出以下内容:
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 }
答案 0 :(得分:0)
要下载目录和文件,请尝试以下解决方法:对所有子目录使用循环并设置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 $ftpdir\n", $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 $sd\n";
}
# download files
$f1->rget();
# Change to upper level
$f1->cwd("..");
}
$f1->quit;