java 如何从java中的url下载图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39641509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download image from from url in java?
提问by coral
I have been trying to download an image from url via java.
I tried to solve it with many ways but I didn't succeed.
我一直在尝试通过java从url下载图像。
我试图用很多方法解决它,但我没有成功。
I'm writing my ways hoping someone will find where it went wrong:
我正在写我的方式,希望有人能找到哪里出错了:
With the first and second ways I get the following error while I'm trying to open the image:
使用第一种和第二种方式,当我尝试打开图像时出现以下错误:
... file appears to be damaged, corrupted, or is too large
... 文件似乎已损坏、损坏或太大
...and its size is smaller than it should be.
...它的大小比它应该的要小。
I guess it's related to encoding.
我想这与编码有关。
First way:
第一种方式:
URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
InputStream in = url.openStream();
Files.copy(in, Paths.get("someFile.jpg"), StandardCopyOption.REPLACE_EXISTING);
in.close();
Second way:
第二种方式:
File f= new File("c:\image.jpg");
URL myUrl = new URL("http://www.avajava.com/images/avajavalogo.jpg");
FileUtils.copyURLToFile(myUrl, f);
With way 3 i get Exception in thread "main" java.lang.IllegalArgumentException: image == null!
使用方式 3 我得到异常 thread "main" java.lang.IllegalArgumentException: image == null!
Third way:
第三种方式:
URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("downloaded.jpg");
ImageIO.write(img, "jpg", file);
I really need your help!!! I have been trying to solve it a long time ago without success.
我真的需要你的帮助!!!很久以前我一直试图解决它,但没有成功。
Thanks in advance!!!
提前致谢!!!
回答by Vadivelan
You have to specify full Location for destination File in way 3,
您必须以方式 3 指定目标文件的完整位置,
URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("D:\image\downloaded.jpg");
ImageIO.write(img, "jpg", file);
Its Working fine for me
它对我来说很好用
回答by fenix121
When this happens usually the image you are trying to download is located inside a placeholder. Placeholders are used to give the image some nice black background in all browsers or any kind of valuable methods..
发生这种情况时,您尝试下载的图像通常位于占位符内。占位符用于在所有浏览器或任何类型的有价值的方法中为图像提供一些漂亮的黑色背景。
Try to open one of your downloaded images with small size with any text editor. You will probably be surprised to see an HTML code inside your supposed image. This HTML code will have a few lines, usually a background color tag and the image real URL. This is the URL that you have to include in your code in order to download the full image.
尝试使用任何文本编辑器打开您下载的小尺寸图像之一。您可能会惊讶地看到您假设的图像中包含 HTML 代码。这个 HTML 代码会有几行,通常是一个背景颜色标签和图片真实 URL。这是您必须包含在代码中才能下载完整图像的 URL。
This could be an example of the avajavalogo.jpgfile that you are actually downloading:
这可能是您实际下载的avajavalogo.jpg文件的示例:
<!DOCTYPE html>
<html>
<body style="background-color:#0e0e0e;">
<center><img src="http://www.myhostsiteisgreat.com/image_real_location_url/awesome_image.jpg" border="0" onclick="Javascript: window.close();" alt="Click on image to close it."></center>
</body>
</html>
If you try to open this file with an image editor, it will throw an error (wrong headers, bad format, etc) and the image will not be opened. It is not the case of the image you have showed in your question, but it could be the case of the images you are actually trying to download.
如果您尝试使用图像编辑器打开此文件,它将引发错误(错误的标题、错误的格式等)并且图像将无法打开。您在问题中显示的图像不是这种情况,但可能是您实际尝试下载的图像的情况。
Apart from that, the methods exposed are valid.
除此之外,公开的方法都是有效的。