public static String getMimeType(String filePath) throws Exception
{
if(filePath == null && filePath.equals(""))
{
throws new Exception("filePath is null or empty");
}
Path tmpPath = Paths.get(filePath);
String contentType = Files.probeContentType(tmpPath);
if(contentType == null && contentType.equals(""))
{
contentType = "application/octet-stream";
}
return contentType;
}
本文介绍了一种通过Java代码获取文件MIME类型的方法。该方法首先检查文件路径是否为空或为null,然后使用Java NIO包中的Files类探查文件的内容类型。如果无法确定内容类型,则默认设置为'application/octet-stream'。
794

被折叠的 条评论
为什么被折叠?



