简体   繁体   English

如何从 byte[] (Blob) 获取文件类型扩展名

[英]How to get file type extension from byte[] (Blob)

How to get file type extension from byte[] (Blob).如何从byte[] (Blob) 获取文件类型扩展名。 I'm reading files from DB to byte[] but i don't know how to automatically detect file extension.我正在从 DB 读取文件到byte[]但我不知道如何自动检测文件扩展名。

Blob blob = rs.getBlob(1);
byte[] bdata = blob.getBytes(1, (int) blob.length());

You mean you want to get the extension of the file for which the blob store the content?你的意思是你想获得 blob 存储内容的文件的扩展名? So if the BLOB stores the content of a jpeg-file, you want "jpg" ?因此,如果 BLOB 存储 jpeg 文件的内容,您想要"jpg"吗?

That's generally speaking not possible.这一般来说是不可能的。 You can make a fairly good guess by 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

它并不完美,但Java Mime Magic 库可能能够推断出文件扩展名:

Magic.getMagicMatch(bdata).getExtension();
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);
            }

尝试使用 ByteArrayDataSource (http://download.oracle.com/javaee/5/api/javax/mail/util/ByteArrayDataSource.html) 你会在那里找到 getContentType() 方法,这应该会有所帮助,但我从未亲自尝试过.

An alternative to using a separate column is using Magic Numbers .使用单独列的另一种方法是使用Magic Numbers Here is some pseudo code:这是一些伪代码:

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.使用幻数的好处是您可以获得实际的文件类型,而不是用户刚刚决定命名的文件类型。

JDKURLConnection类中有不错的方法,请参考以下答案: Getting A File's Mime Type In Java

I know this is a 10 year old post but if stumble here like myself and I, truly speaking there is still no method/property that returns a blob file extension directly.我知道这是一篇已有 10 年历史的帖子,但如果像我和我一样在这里绊倒,说真的,仍然没有直接返回 blob 文件扩展名的方法/属性。 However, with modern javascript you can be able to get blob mime type using type property.但是,使用现代 javascript,您可以使用type属性获取 blob mime type Then you can split the mime type and get the last array value like this: (blob.type).split('/')[1] .然后您可以拆分 mime 类型并获取最后一个数组值,如下所示: (blob.type).split('/')[1] When we use split on a string, you get an array, blob type will always yield an array of 2, that why we are using [1] to grab the last value which is now becomes our file/image extension.当我们对字符串使用 split 时,您会得到一个数组,blob 类型将始终产生一个 2 的数组,这就是我们使用[1]来获取最后一个值的原因,该值现在成为我们的文件/图像扩展名。 Consider:考虑:

// Create a jpeg blob if you dont have one yet
const blob = await new Promise(rs => canvas.toBlob(rs, 'image/jpeg', 1,));
// grab the blob type
const blobType = blob.type
// get the image extension as explained above
const blobExtension = blobType.split('/')[1] // returns a jpg
// set image name
const imageName = `your_image_string_here.${blobExtension}`
// Results >> your_image_string_here.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM