如何在java中从网络加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/926938/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:11:45  来源:igfitidea点击:

how to load a image from web in java

javaimage

提问by

I need to load an image from a web in a simple Java stand alone aplication. Any Ideas?

我需要在一个简单的 Java 独立应用程序中从网络加载图像。有任何想法吗?

采纳答案by Gary Kephart

URL url = new URL("http://host/theimage.jpg");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();

is that enough to start you? Don't know what you want to do from there.

这足以让你开始吗?不知道你想从那里做什么。

回答by Brian Agnew

I would take a look at HTTPClient.

我会看看HTTPClient

Find the URL to the image, and you can get an inputstream feeding you the image data, plus you'll get the content-type etc. so you can correctly handle it once you've downloaded it.

找到图像的 URL,您可以获得一个输入流为您提供图像数据,此外您还将获得内容类型等,这样您就可以在下载后正确处理它。

Here'ssome sample code. You may also need to call getResponseHeaders() on the GetMethodto identify the image type.

这是一些示例代码。您可能还需要在GetMethod上调用 getResponseHeaders()来识别图像类型。

回答by iny

回答by rodion

You can load an image using

您可以使用加载图像

BufferedImage img = ImageIO.read(new URL("http://stackoverflow.com/content/img/so/logo.png"));

For methods how to display the loaded image, see the Sun "Working with images" tutorial.

有关如何显示加载图像的方法,请参阅 Sun “使用图像”教程