我正在将文件上传到Amazon s3存储桶,并且可以访问InputStream和包含文件的MIME类型但不包含原始文件名的String.在将文件推送到S3之前,实际创建文件名和扩展名取决于我.是否有库或方便的方法来确定MIME类型中使用的适当扩展名?
我已经看到一些对Apache Tika库的引用,但这看起来有点过分,我还没有能够成功检测到文件扩展名.从我能够收集的内容来看,这段代码看起来应该可行,但是当我的类型变量是"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
}
Run Code Online (Sandbox Code Playgroud)