Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could consider using mime-utils. It basically gives you the mime type for a given byte[] or stream source using the same detection that the unix <code>file</code> command uses.</p> <p><a href="http://www.medsea.eu/mime-util" rel="nofollow noreferrer">http://www.medsea.eu/mime-util</a> (seems to be unavailable currently, try looking at the google cache for info).</p> <p>You could also consider simply parsing every image using ImageIO.read and then rewriting them all using ImageIO.write. This will work for jpeg images too, althogh it isn't totally efficient. It would be an appropriate solution if you wanted to scale, crop or watermark your images at the same time. Since this is web submitted data, there is a certain argument for attempting to parse the file as image to make sure that it really is an image, and not a malicious file type.</p> <p>ImageIO is smart so you don't have to know what type the data is to begin with. And it will through an exception if the data is malformed, so this makes sure that your image data is valid.</p> <p>JPEG files apparently start with 0xFFD8, so you can just check for that in your code.</p> <p>For example, to simply recode every image that doesn't look like a jpeg:</p> <pre><code>byte[] bytes = new byte[1024]; //your input byte[] result = bytes; if( bytes[0] != 0xFF || bytes[1] != 0xD8 ) { BufferedImage image = ImageIO.read( new ByteArrayInputStream( bytes ) ); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write( image, "jpeg", baos ); result = baos.toByteArray(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload