如何从 Java 中的 MIME 类型确定适当的文件扩展名 [英] How to determine appropriate file extension from MIME Type in Java

查看:52
本文介绍了如何从 Java 中的 MIME 类型确定适当的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文件上传到 Amazon s3 存储桶,并且可以访问 InputStream 和包含文件 MIME 类型但不包含原始文件名的字符串.在将文件推送到 S3 之前,由我实际创建文件名和扩展名.是否有库或方便的方法来确定要从 MIME 类型中使用的适当扩展名?

I am uploading files to an Amazon s3 bucket and have access to the InputStream and a String containing the MIME Type of the file but not the original file name. It's up to me to actually create the file name and extension before pushing the file up to S3. Is there a library or convenient way to determine the appropriate extension to use from the MIME Type?

我已经看到了一些对 Apache Tika 库的引用,但这似乎有点矫枉过正,我还没有能够成功检测到文件扩展名.从我收集到的信息来看,这段代码应该可以工作,但是当我的类型变量是image/jpeg"时,我得到的是一个空字符串

I've seen some references to the Apache Tika library but that seems like overkill and I haven't been able to get it to successfully detect file extensions yet. From what I've been able to gather it seems like this code should work, but I'm just getting an empty string when my type variable is "image/jpeg"

    MimeType mimeType = null;
    try {
        mimeType = new MimeTypes().forName(type);
    } catch (MimeTypeException e) {
        Logger.error("Couldn't Detect Mime Type for type: " + type, e);
    }

    if (mimeType != null) {
        String extension = mimeType.getExtension();
        //do something with the extension
    }

推荐答案

正如一些评论者所指出的,在 mimetypes 和文件扩展名之间没有通用的 1:1 映射......一些 mimetypes 有不止一种可能的扩展名,很多扩展被多个mimetype共享,有些mimetype没有扩展.

As some of the commentors have pointed out, there is no universal 1:1 mapping between mimetypes and file extensions... Some mimetypes have more than one possible extension, many extensions are shared by multiple mimetypes, and some mimetypes have no extension.

在可能的情况下,最好存储 mimetype 并继续使用它,而忘记扩展名.

Wherever possible, you're much better off storing the mimetype and using that going forward, and forgetting about the extension.

也就是说,如果您确实想为给定的 mimetype 获取最常见的文件扩展名,那么 Tika 是一个不错的选择.Apache Tika 有大量它知道的 mimetypes,并且对于其中许多它也知道 mime magic for检测、常用扩展、描述等

That said, if you do want to get the most common file extension for a given mimetype, then Tika is a good way to go. Apache Tika has a very large set of mimetypes it knows about, and for many of these it also knows mime magic for detection, common extensions, descriptions etc.

如果你想获得 JPEG 文件最常用的扩展名,那么如 这个 Apache Tika 单元测试 你只需要做一些类似的事情:

If you want to get the most common extension for a JPEG file, then as shown in this Apache Tika unit test you just need to do something like:

  MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
  MimeType jpeg = allTypes.forName("image/jpeg");
  String jpegExt = jpeg.getExtension(); // .jpg
  assertEquals(".jpg", jpeg.getExtension());

关键是您需要加载捆绑在 Tika jar 中的 xml 文件以获取所有 mimetypes 的定义.如果您也可能处理自定义 mimetypes,那么 Tika 支持这些,并将第一行更改为:

The key thing is that you need to load up the xml file that's bundled in the Tika jar to get the definitions of all the mimetypes. If you might be dealing with custom mimetypes too, then Tika supports those, and change line one to be:

  TikaConfig config = TikaConfig.getDefaultConfig();
  MimeTypes allTypes = config.getMimeRepository();

通过使用 TikaConfig 方法获取 MimeTypes,Tika 还将检查您的类路径以获取自定义 mimetype 定义,并将这些定义也包括在内.

By using the TikaConfig method to get the MimeTypes, Tika will also check your classpath for custom mimetype defintions, and include those too.

这篇关于如何从 Java 中的 MIME 类型确定适当的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆