简体   繁体   English

如何使用java检查文件是否是png图像文件

[英]how to check if a file is an png image file,using java

I am building an automate png to jpg, everything is built, but I have a problem detecting png files.我正在构建一个自动 png 到 jpg,一切都已构建,但我在检测 png 文件时遇到问题。 Now I am using the file name: I am checking if the end of the file matches .png but this is not working for png files that do not end with .png .现在我正在使用文件名:我正在检查文件的结尾是否与.png匹配,但这不适用于不以.png结尾的 png 文件。

Any ideas?有什么想法吗?

You can check the header of the file.您可以检查文件的标题。
See http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header .请参阅http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header

u can try this你可以试试这个

import javax.activation.MimetypesFileTypeMap;
import java.io.File;

class GetMimeType {
  public static void main(String args[]) {
    File f = new File(filePath);
    System.out.println("Mime Type of " + f.getName() + " is " +
                         new MimetypesFileTypeMap().getContentType(f));

}

or

try试试

public String getContentType(File file) throws IOException {
        return Files.probeContentType(file.getAbsolutePath());
}

您可以使用Files.probeContentType(path)来获取 MIME 类型

I downloaded some images from a website using a crawler.我使用爬虫从网站下载了一些图像。 All of the images were listed in the source as JPG, but there were some PNG mixed in. The browser is flexible, because the web is messy, but not all desktop applications process PNG files with a .jpg extension correctly.所有的图片在源代码中都被列为JPG,但也有一些PNG 混入。浏览器很灵活,因为网络很乱,但并不是所有的桌面应用程序都能正确处理带有.jpg 扩展名的PNG 文件。 I tried the various solutions in the accepted answer, but all of them use the file name for identification of the MIME type.我在接受的答案中尝试了各种解决方案,但所有解决方案都使用文件名来标识 MIME 类型。 I checked the source (java8) of MimetypesFileTypeMap and even if you pass it a File, it just gets the file name and uses that the determine the mime type.我检查了 MimetypesFileTypeMap 的源代码 (java8),即使您传递给它一个文件,它也只会获取文件名并使用它来确定 mime 类型。

So instead I opted to read the 4 byte file header to determine whether it was a JPG or PNG as suggested by Itay Karo.因此,我选择读取 4 字节文件头,以确定它是 Itay Karo 建议的 JPG 还是 PNG。 For JPG the first byte would be FF, for PNG it would be 89 (HEX).对于 JPG,第一个字节将是 FF,对于 PNG,它将是 89 (HEX)。

private static boolean isPng(File file) throws IOException {
    try (FileInputStream is = new FileInputStream(file)) {
        return Integer.toHexString(is.read).equals("89");
    }
}

which is equal to 139 (base 10), so a slightly more efficient solution is:等于 139(以 10 为底),因此稍微更有效的解决方案是:

private static boolean isPng(File file) throws IOException {
    try (FileInputStream is = new FileInputStream(file)) {
        return is.read() == 137;
    }
}

Note that this enough to differentiate between JPG and PNG, but you might want to check the rest of the bytes as well for more accuracy, if you really don't know what kind of files you are dealing with.请注意,这足以区分 JPG 和 PNG,但如果您真的不知道要处理的文件类型,您可能还想检查其余字节以获得更高的准确性。 Props to Itay Karo for providing a description of the PNG header: Itay Karo 提供 PNG 标头描述的道具:

http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header

Oh, and one more thing I should mention.哦,还有一件事我应该提一下。 In Linux you can use "file" to query the mime type:在 Linux 中,您可以使用“file”来查询 mime 类型:

file -i [file]

Not an answer to the question as it is phrased, but it might be easier to write a simple shell script that runs after your java application:不是问题的答案,因为它的措辞,但编写一个在 Java 应用程序之后运行的简单 shell 脚本可能更容易:

for file in $(find "$root" -name '*.jpg')
do
        if file -i "$file" | grep -q 'image/png'; then
                rename="${file%.*}.png"
                echo "moving $file -> $rename"
                mv $file $rename
        fi
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM