java 如何从 byte[] (Blob) 获取文件类型扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7120456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file type extension from byte[] (Blob)
提问by senzacionale
How to get file type extension from byte[]
(Blob). I'm reading files from DB to byte[]
but i don't know how to automatically detect file extension.
如何从byte[]
(Blob)获取文件类型扩展名。我正在从数据库读取文件,byte[]
但我不知道如何自动检测文件扩展名。
Blob blob = rs.getBlob(1);
byte[] bdata = blob.getBytes(1, (int) blob.length());
回答by aioobe
You mean you want to get the extension of the file for which the blob store the content? So if the BLOB stores the content of a jpeg-file, you want "jpg"
?
您的意思是要获取 blob 存储内容的文件的扩展名?因此,如果 BLOB 存储 jpeg 文件的内容,您想要"jpg"
吗?
That's generally speaking not possible. You can make a fairly good guessby using some heuristic such as Apache Tikas content detection.
这一般来说是不可能的。您可以通过使用一些启发式方法(例如Apache Tikas 内容检测)来做出相当好的猜测。
A better solution however, would be to store the mime type (or original file extension) in a separate column, such as a VARCHAR
.
然而,更好的解决方案是将 MIME 类型(或原始文件扩展名)存储在单独的列中,例如VARCHAR
.
回答by Adam Paynter
It's not perfect, but the Java Mime Magic librarymay be able to infer the file extension:
它并不完美,但Java Mime Magic 库可能能够推断出文件扩展名:
Magic.getMagicMatch(bdata).getExtension();
回答by Kris
Try with ByteArrayDataSource (http://download.oracle.com/javaee/5/api/javax/mail/util/ByteArrayDataSource.html) you will find getContentType() method there, which should help but I've never tried it personally.
尝试使用 ByteArrayDataSource (http://download.oracle.com/javaee/5/api/javax/mail/util/ByteArrayDataSource.html) 你会在那里找到 getContentType() 方法,这应该有帮助,但我从来没有亲自尝试过.
回答by Quoendithas
An alternative to using a separate column is using Magic Numbers. Here is some pseudo code:
使用单独列的替代方法是使用Magic Numbers。下面是一些伪代码:
getFileExtn(BLOB)
{
PNGMagNum[] = {0x89, 0x50, 0x4E, 0x47}
if(BLOB[0:3] == PNGMagNum)
return ".png"
//More checks...
}
You would have to do this for every file type you support. Some obscure file types you might have to find out yourself via a hex editor (the magic number is always the first few bytes of code). The benefit of using the magic number is you get the actual file type, and not what the user just decided to name it.
您必须为支持的每种文件类型执行此操作。您可能需要通过十六进制编辑器自己找出一些晦涩的文件类型(幻数总是代码的前几个字节)。使用幻数的好处是您可以获得实际的文件类型,而不是用户刚刚决定命名的文件类型。
回答by Akin Okegbile
if(currentImageType ==null){
ByteArrayInputStream is = new ByteArrayInputStream(image);
String mimeType = URLConnection.guessContentTypeFromStream(is);
if(mimeType == null){
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
mimeType = detector.detect(is,md).toString();
if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
}
if(mimeType.contains("png")){
mimeType ="png";
}
else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
mimeType = "jpg";
}
else if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
currentImageType = ImageType.fromValue(mimeType);
}
回答by Yuriy Nakonechnyy
There is decent method in JDK
's URLConnection
class, please refer to following answer:
Getting A File's Mime Type In Java
有体面的方法JDK
的URLConnection
课程,请参考以下的答案:
获取文件的MIME类型在Java