简体   繁体   English

Java如何手动更改文件的MIME类型

[英]Java how to manually change MIME type of a file

I have a .m4a file that needs to be of mime type "audio/mp4" currently it is "video/mp4". 我有一个.m4a文件,该文件需要为mime类型“ audio / mp4”,当前为“ video / mp4”。

When I execute "file Rooo.m4a -i" 当我执行“文件Rooo.m4a -i”时

Rooo.m4a: video/mp4; Rooo.m4a:视频/ mp4; charset=binary 字符集=二进制

Is it possible to change it manually at runtime? 是否可以在运行时手动更改它?

I suppose (from command file Rooo.m4a -i ) that you're using some linux OS. 我想(从命令file Rooo.m4a -i )您正在使用某些Linux OS。 Java uses normal linux configuration where file types are guessed by /etc/mime.types and $HOME/.mime.types . Java使用正常的linux配置,其中/etc/mime.types$HOME/.mime.types猜测文件类型。 Are you really sure you need to change it at runtime? 您确定要在运行时进行更改吗? Possible solution is to change system mime.types; 可能的解决方案是更改系统mime.types; this page can be useful in it. 此页面可能很有用。

If you're sure you probably need to check documentation for Files.probeContentType method. 如果确定您可能需要检查Files.probeContentType方法的文档。 It states that you need to define your own FileTypeDetector subclasses and list them in META-INF/services . 它指出您需要定义自己的FileTypeDetector子类,并将它们列出在META-INF/services

File type detectors are typically installed by placing them in a JAR file on the application class path or in the extension directory, the JAR file contains a provider-configuration file named java.nio.file.spi.FileTypeDetector in the resource directory META-INF/services, and the file lists one or more fully-qualified names of concrete subclass of FileTypeDetector that have a zero argument constructor. 文件类型检测器通常通过将它们放置在应用程序类路径或扩展目录中的JAR文件中进行安装,该JAR文件在资源目录META-INF中包含名为java.nio.file.spi.FileTypeDetector的提供程序配置文件。 / services,该文件列出一个或多个具有零参数构造函数的FileTypeDetector具体子类的标准名称。

Defining you own FileTypeDetector should solve the problem because it checks file type before the default one does. 定义自己的FileTypeDetector应该可以解决该问题,因为它会在默认文件类型之前检查文件类型。

See also: Getting A File's Mime Type In Java 另请参阅: 在Java中获取文件的Mime类型

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

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