如何在java中从web加载图像

时间:2023-01-24 16:20:55

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

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

4 个解决方案

#1


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.

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

#2


You can load an image using

您可以使用加载图像

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

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

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

#4


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's some sample code. You may also need to call getResponseHeaders() on the GetMethod to identify the image type.

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

#1


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.

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

#2


You can load an image using

您可以使用加载图像

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

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

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

#3


#4


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's some sample code. You may also need to call getResponseHeaders() on the GetMethod to identify the image type.

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