Java get image extension from byte array

后端 未结 2 522
醉话见心
醉话见心 2021-01-05 06:50

I have the below code for saving an image from a byte array.

I am able to save the image successfully using the below code.

Currently I am saving image with

相关标签:
2条回答
  • 2021-01-05 06:58

    There are many solutions. Very simple for example:

    String contentType = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(imageBytes));
    

    Or you can use third-party libraries. Like Apache Tika:

    String contentType = new Tika().detect(imageBytes);
    
    0 讨论(0)
  • 2021-01-05 07:08

    Would you use the prefix header data:image/png;base64 of the base64 image data?

    From the RFC, the "data" url definition:

    ### Form : `data:[<mediatype>][;base64],<data>`
    ### Syntax:
    `dataurl   := "data:" [ mediatype ] [ ";base64" ] "," data
    mediatype:= [ type "/" subtype ] *( ";" parameter )
    data       := *urlchar
    parameter  := attribute "=" value`
    

    Samples:

    data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw......
    

    Then you can get the extension with gif.

    0 讨论(0)
提交回复
热议问题