我有一个 SOAP-WebService,它提供了一种方法,调用者可以在其中上传 PDF、JPG、PNG 或 BMP 文件。 为了正确处理,我需要从 DataHandler 中获取 MIME 类型。 我尝试使用 Apache Tika 获取 MIME 类型:
Tika tika = new Tika();
InputStream stream = dataHandler.getInputStream();
String mimeType = tika.detect(stream);
现在我的问题:
大多数情况下,Tika 会检测到正确的 MIME 类型,但在少数上传 JPG 的情况下,它会检测到 plain/text
而不是 image/jpeg
。我该如何解决这个问题?
提前谢谢您!
最佳答案
我不知道 Tika 有什么问题,但作为替代方案,你可以尝试 MimeUtil它的功能几乎相同,并且做得很好,并且具有更大的灵 active ,因为您可以轻松配置它。
// Define the mime type detector to use, here it will be MagicMimeMimeDetector
// As you intend to detect from a Stream
// To be done only once in a static block of your class for example
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
...
// Get the collection of matching mime types
Collection<?> mimeTypes = MimeUtil.getMimeTypes(stream);
有关该类的更多详细信息MimeUtil
here .
关于java - 从 DataHandler 检测 MIME 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39118255/