如何在不知道文件扩展名的情况下获取图像类型?
问题描述:
我需要知道没有文件扩展名的文件扩展名?这是否可以使用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));