Why does my image download with Perl's LWP give me the wrong-sized file?

大憨熊 提交于 2019-12-08 19:26:46

问题


I am trying to get an image from an HTTP server using Perl.

I have the full URL of the file and am attempting to use

my $data = LWP::Simple::get $params{URL};
my $filename = "image.jpg";
open (FH, ">$filename");
print FH $data;
close (FH);

Now, logically, to me at least, this should work. But the files are slightly different sizes, and I can't work out why.

Help!


回答1:


You need to use binmode to properly write the image data to disk.

my $data = LWP::Simple::get $params{URL};
my $filename = "image.jpg";
open (FH, ">$filename");
binmode (FH);
print FH $data;
close (FH);

Otherwise it is interpreted as text, and the newlines get munged.




回答2:


Dave is right, you should/must set your file handle to binary mode. But you could do all that in one go:

LWP::Simple::getstore( $params{URL}, 'image.jpg' );


来源:https://stackoverflow.com/questions/926457/why-does-my-image-download-with-perls-lwp-give-me-the-wrong-sized-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!