Java使用URL中的BufferedImage获取图像扩展名/类型 [英] Java get image extension/type using BufferedImage from URL

查看:5033
本文介绍了Java使用URL中的BufferedImage获取图像扩展名/类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉使用图片。我检索/读取网址中的图片,其中包含网址没有文件扩展名。然后我希望将图像写入/保存到本地存储,但我必须指定图像文件扩展名(即JPG,PNG等),我无法通过BufferedImage检索其扩展名。

I am familiar with working with images. I retrieve/read an image from a URL, where the URL does not have a file extension. Then I wish to write/save the image to the local storage, but I have to specify the image file extension (i.e. JPG, PNG, etc.), which I cannot retrieve its extension though the BufferedImage.

可以请指出怎么做?
任何其他方法都可以。

Could one please point out how it can be done? Any other method will do.

推荐答案

使用 ImageReader.getFormatName()

您可以使用 ImageIO.getImageReaders(Object input)

我自己没有测试过,但你可以试试这个:

I haven't tested it myself, but you can try this:

ImageInputStream iis = ImageIO.createImageInputStream(file);

Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);

while (imageReaders.hasNext()) {
    ImageReader reader = (ImageReader) imageReaders.next();
    System.out.printf("formatName: %s%n", reader.getFormatName());
}

这篇关于Java使用URL中的BufferedImage获取图像扩展名/类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆