如何在不知道文件扩展名的情况下获取图像类型? [英] How to get an image type without knowing its file extension?
本文介绍了如何在不知道文件扩展名的情况下获取图像类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道没有文件扩展名的文件扩展名?这是否可以使用java IO API?
I need to know a file extension without having the file extension? Is this possible with the java IO API?
我在读取JPG文件时遇到以下异常:
I'm getting the following exception while reading a JPG file:
javax.imageio.IIOException: Unsupported Image Type
任何人有任何想法如何解决这个问题?
Does anyone have any ideas how to solve this?
当我尝试在Mac OS X或Windows中的普通图像查看器中打开图像时,它的工作正常。
When i try to open the image in a normal image viewer in Mac OS X or Windows, it works just fine.
是否有可能将文件重命名为其他类型且原始类型与扩展名不同?
Is there any chances the file was renamed to a different type and it's original type is different from the extension?
推荐答案
您可以使用此代码获取mime类型。
You can get the mime type with this code.
String imageName = "C:\\Documents and Settings\\home\\Desktop\\P092870.jpg";
File input = new File(imageName);
System.out.println("System Type description of " + input.getName() + " is " +
FileSystemView.getFileSystemView().getSystemTypeDescription(input));
System.out.println("Mime Type of " + input.getName() + " is " +
new MimetypesFileTypeMap().getContentType(input));
这篇关于如何在不知道文件扩展名的情况下获取图像类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文