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

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

问题描述

我将文件上传到亚马逊的S3存储和访问的InputStream和包含文件的MIME类型,但不是原来的文件名的字符串。它是由我来实际创建的文件名和扩展名推文件直到S3之前。是否有一个图书馆或方便的方式来确定适当的扩展名的MIME类型使用?

我已经看到了一些引用了Apache提卡库,但似乎有点小题大做,我一直没能得到它成功地检测出文件扩展名呢。从我已经能够收集它似乎像这样code应该工作,但我刚开始的时候我喜欢的类型变量是为image / jpeg

一个空字符串

  Mime类型MIMETYPE = NULL;
    尝试 {
        MIMETYPE =新的MIME类型()的forName(类型)。
    }赶上(MimeTypeException E){
        Logger.error(无法检测MIME类型类型:+型,E);
    }

    如果(MIMETYPE!= NULL){
        字符串延长= mimeType.getExtension();
        //做一些与扩展
    }
 

解决方案

由于有些评论者都指出,没有普遍的1:MIME类型和文件扩展名之间的一对一映射...一些MIME类型有多个可能的扩展,很多扩展由多个MIME类型的共享,以及一些MIME类型没有扩展名。

只要有可能,你好得多存储MIMETYPE,并使用该前进,而忘记了扩展。

这是说,如果你想获得最常见的文件扩展名对于一个给定mime类型,然后提卡是一个很好的路要走。 阿帕奇提卡有一个非常大集就知道MIME类型,而对于许多这些它也知道哑剧魔术进行检测,常用的扩展,说明等。

如果你想获得最常见的扩展JPEG文件,然后如图<一href="http://svn.apache.org/repos/asf/tika/trunk/tika-core/src/test/java/org/apache/tika/mime/PatternsTest.java">this阿帕奇提卡单元测试你只需要做的是这样的:

  MIMETYPES allTypes = MimeTypes.getDefaultMimeTypes();
  Mime类型JPEG = allTypes.forName(为image / jpeg);
  串jpegExt = jpeg.getExtension(); // .JPG
  的assertEquals(。JPG,jpeg.getExtension());
 

最关键的一点是,你需要加载了多数民众赞成捆绑在提卡罐子把所有的MIME类型的定义的XML文件。如果你可能会使用自定义MIME类型处理过,然后提卡支持这些,变线之一是:

  TikaConfig配置= TikaConfig.getDefaultConfig();
  MIME类型allTypes = config.getMimeRepository();
 

通过使用TikaConfig方法获取MIME类型,提卡也将检查您的类路径的自定义MIMETYPE defintions,并包括了。

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?

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
    }

解决方案

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.

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

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.

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());

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();

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

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

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