我正在构建一个自动 png 到 jpg,一切都已构建,但我在检测 png 文件时遇到问题。现在我正在使用文件名:我正在检查文件的结尾是否匹配,.png但这不适用于不以.png.
有任何想法吗?
你可以试试这个
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));
}
Run Code Online (Sandbox Code Playgroud)
或者
尝试
public String getContentType(File file) throws IOException {
return Files.probeContentType(file.getAbsolutePath());
}
Run Code Online (Sandbox Code Playgroud)