我尝试使用Files.probeContentType,但是它随机返回null,并且我已经在java版本中读取了它在Windows 8上的bug。还有其他选择吗?
答案 0 :(得分:0)
URLConnection.getContentType(),一个“返回值的方法” 内容类型标题字段。“
这对我来说很好:
public String identifyFileTypeUsingUrlConnectionGetContentType(final String fileName)
{
String fileType = "Undetermined";
try
{
final URL url = new URL("file://" + fileName);
final URLConnection connection = url.openConnection();
fileType = connection.getContentType();
}
catch (MalformedURLException badUrlEx)
{
System.out.println("ERROR: Bad URL - " + badUrlEx);
}
catch (IOException ioEx)
{
System.out.println("Cannot access URLConnection - " + ioEx);
}
return fileType;
}