如何在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
how to load a image from web in java
提问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.
回答by iny
See ImageIO.read(URL).
回答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 “使用图像”教程。