java - 为什么 MimetypesFileTypeMap 总是为 PNG 文件返回 "application/octet-stream"?

标签 java mime

我正在尝试使用 javax.activation.MimetypesFileTypeMap 获取内容类型。

对于字符串“image.png”,它总是返回“application/octect-stream”……它不应该返回类似“image/png”的东西吗?

javax.activation.MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType("image.png");

最佳答案

参见 javax.activation.MimetypesFileTypeMap 的 Javadocs .该方法查找名为 .mime.types 的文件按一定顺序:

MIME types file search order:

The MimetypesFileTypeMap looks in various places in the user's system for MIME types file entries. When requests are made to search for MIME types in the MimetypesFileTypeMap, it searches MIME types files in the following order:

  1. Programmatically added entries to the MimetypesFileTypeMap instance.
  2. The file .mime.types in the user's home directory.
  3. The file <java.home>/lib/mime.types.
  4. The file or resources named META-INF/mime.types.
  5. The file or resource named META-INF/mimetypes.default (usually found only in the activation.jar file).

如果没有找到文件,getContentType方法返回 application/octet-stream :

Return the MIME type based on the specified file name. The MIME type entries are searched as described above under MIME types file search order. If no entry is found, the type "application/octet-stream" is returned.

关于java - 为什么 MimetypesFileTypeMap 总是为 PNG 文件返回 "application/octet-stream"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34165873/

相关文章:

java - 如何使用 DocuSign API 预填充服务器模板上的选项卡

java - 为辅助鼠标按钮添加鼠标事件 Javafx

java - 如何使用 hasNextInt() 捕获异常?我需要 Int 但如果输入是字符,那就不好了

JavaScript:创建 MIME 消息

java - 解析JSON字符串的最简单方法

java - JPanel TitledBorder 中的标题截断 - Java swing

java - 如何从 Java 中的 HttpResponse 获取单个表单字段并将其写入文件?

python - 如何通过Python 3.5将base64图像嵌入到HTML电子邮件中?

google-apps-script - 是否可以从另一个脚本读取 Google Apps 脚本的内容

java - 用 Java 解析 MIME 发送方 (RFC 822)