[英]Reading file type (MIME) returns null in Java
I'm editing a custom CSV importer that doesn't want to run on my system. 我正在编辑一个不想在我的系统上运行的自定义CSV导入器。 My version of Java is: "1.8.0_171". 我的Java版本是:“ 1.8.0_171”。
String contentType = Files.probeContentType(filePath);
//Should return "text/csv";
System.out.println("You have "+contentType+" type file.");
//returns "You have null type file."
I've checked whether the file is accessible - it returns true. 我检查了文件是否可访问-它返回true。
System.out.println( "File is readable: " + Files.isReadable(filePath));
// returns: "File is readable: true"
What steps should I take next? 接下来我应该采取什么步骤? I found some similar bugs: 我发现了一些类似的错误:
https://bugs.java.com/view_bug.do?bug_id=JDK-7144997 https://bugs.openjdk.java.net/browse/JDK-8080369 https://bugs.java.com/view_bug.do?bug_id=JDK-7144997 https://bugs.openjdk.java.net/browse/JDK-8080369
I don't know whether they are related however. 我不知道他们是否相关。
UPDATE Updating Java to version "10.0.1" did not help. UPDATE将 Java更新到版本“ 10.0.1”没有帮助。
UPDATE 2 Adding null to the list of allowed file types did work, however I consider this a HUGE hack, and this still needs resolving. 更新2向允许的文件类型列表中添加null确实可行,但是我认为这是一个巨大的漏洞,并且仍然需要解决。
On Windows without any additional FileTypeDetector
implementations installed Java will use default file type detector RegistryFileTypeDetector
, which will return whatever has been associated with that extension on that particular machine, meaning whatever is under HKEY_CLASSES_ROOT\\.csv
is returned. 在没有安装任何其他FileTypeDetector
实现的Windows上,Java将使用默认文件类型检测器RegistryFileTypeDetector
,它将返回该特定计算机上与该扩展名关联的所有内容,这意味着将返回HKEY_CLASSES_ROOT\\.csv
下的所有内容。
If you want to enable some software to run consider installing custom FileTypeDetector
on classpath via ServiceLoader
mechanism or associating .csv
with required value in registry. 如果要启用某些软件,请考虑通过ServiceLoader
机制在类路径上安装自定义FileTypeDetector
,或将.csv
与注册表中的必需值相关联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.