如何从 Java 中的 URL 读取图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3986891/
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 can I read image from URL in Java?
提问by newbie
I have servlet in my web application that serves images, and when I visit those urls with browser images are server correctly. Then I have this other servlet that resizes images, idea is to visit get image by url in resize servlet and then resize image. But for some reason all following methods return null, but when I visit given url with browser, image is shown correctly.
我的 web 应用程序中有一个 servlet 来提供图像,当我访问那些带有浏览器图像的 url 时,服务器是正确的。然后我有另一个调整图像大小的 servlet,想法是在调整大小的 servlet 中通过 url 访问获取图像,然后调整图像大小。但是由于某种原因,以下所有方法都返回 null,但是当我使用浏览器访问给定的 url 时,图像显示正确。
URL imageURL = new URL(fullUrl);
// Case 1
RenderedImage img = ImageIO.read(imageURL);
// Case 2
BufferedImage img = JAI.create("url", imageURL).getAsBufferedImage();
// Case 3
Image img = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(imageURL);
采纳答案by Jigar Joshi
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
Image image = ImageIO.read(url);
or
或者
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
Update:
更新:
This code works for me Try checking your URL.
此代码对我有用 尝试检查您的 URL。
public static void main(String[] args) throws Exception {
URL imageURL = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
// Case 1
RenderedImage img = ImageIO.read(imageURL);
System.out.println(img);
}
output:
输出:
BufferedImage@e80a59: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 col
or space = java.awt.color.ICC_ColorSpace@1ff5ea7 transparency = 1 has alpha = fa
lse isAlphaPre = false ByteInterleavedRaster: width = 553 height = 737 #numDataE
lements 3 dataOff[0] = 2
回答by T.J. Crowder
回答by Googlian
Displays an image read from a URL within a JFrame. urlLocation
URL pointing to an image.
显示从 JFrame 中的 URL 读取的图像。 urlLocation
指向图像的 URL。
public class ShowImageFromURL {
public static void show(String urlLocation) {
Image image = null;
try {
URL url = new URL(urlLocation);
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
conn.connect();
InputStream urlStream = conn.getInputStream();
image = ImageIO.read(urlStream);
JFrame frame = new JFrame();
JLabel lblimage = new JLabel(new ImageIcon(image));
frame.getContentPane().add(lblimage, BorderLayout.CENTER);
frame.setSize(image.getWidth(null) + 50, image.getHeight(null) + 50);
frame.setVisible(true);
} catch (IOException e) {
System.out.println("Something went wrong, sorry:" + e.toString());
e.printStackTrace();
}
}
}
Ref: https://gist.github.com/aslamanver/92af3ac67406cfd116b7e4e177156926
参考:https: //gist.github.com/aslamanver/92af3ac67406cfd116b7e4e177156926