我正在尝试创建使用模块File::Fetch下载的文件的哈希值。我正在尝试将下载的文件名及其哈希值存储在数据库中作为记录。但是,File :: Fetch对象的方法都不提供下载的文件。有没有办法将文件下载到变量中以创建它的哈希值?
以下是我的代码片段
eval{
$uri_handle = File::Fetch->new(uri => $nurl) or die "Could'nt create fetch object\n";
$getfile = $uri_handle ->output_file or die "There is no file to be fetched\n";
my $dir_handle = $uri_handle->fetch( to => $dir2 ) or die "Couldn't fetch file: $uri_handle->error\n";
# print "$getfile was successfully saved to $dir\n";
};
if ($@){
print "\n There is no file at $url\n\n";
}else{
my $file_data_handle = DBI->connect($database_connection_string,$database_user,$database_pass) or die "Couldn't open database: $DBI::errstr\n";
my $file_statement_handle = $file_data_handle->prepare('insert into files (filename,filehash,sourceurl,originalurl) VALUES ($getfile,$filehash,$nurl,$url)') or die "Couldn't prepare statement: $DBI::errstr\n";
$file_statement_handle->execute() or die "Couldn't execute statement: $DBI::errstr\n";
$file_data_handle->disconnect();
}
答案 0 :(得分:3)
它直接将结果写入磁盘。请改用LWP。
use Digest::MD5 qw( md5_hex );
use LWP::UserAgent qw( );
my $ua = LWP::UserAgent->new();
my $response = $ua->get($url);
die $response->status_line if !$response->is_success;
my $file = $response->decoded_content( charset => 'none' );
my $md5_hex = md5_hex($file);