@Test public void testGetMimeType() { int counter = 0; for (String filePath : new File(TEST_FILES_ROOT).list()) { File file = new File(TEST_FILES_ROOT + "/" + filePath); if (file.isFile() && !file.getName().equals("License.txt")) { counter++; String mimeType = MimeTypeHelper.getMimeType(file); boolean contains = file.getName().contains( mimeType.replace("/", "_").replace("*", "_")); System.out.println("MimeTypeHelperTest: " + file.getName() + " => " + mimeType + " " + contains + " "); Assert.assertTrue(contains, file.getName() + " => " + mimeType + "(Already " + (counter - 1) + " successfully tested)"); } } System.out.println("Tested " + counter + " files for MimeType detection."); }
public static String getMimeType(byte[] fileData) { @SuppressWarnings("rawtypes") Collection mimeTypes = MIME_UTIL.getMimeTypes(fileData); String mimeType = MimeUtil2.getMostSpecificMimeType(mimeTypes).toString(); if (mimeType.startsWith("image/")) { try { Imaging.getImageInfo(fileData); return mimeType; } catch (ImageReadException e) { LOGGER.error("Imaging: Image seems to be invalid or unsupported", e); } catch (IOException e) { LOGGER.error("Imaging: There was an error reading the image", e); } ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData); try { ImageIO.read(inputStream).getAlphaRaster(); } catch (Exception e) { LOGGER.error("There was an error reading the image: {}", e); mimeType = "image/x-unknown"; } finally { IOUtils.closeQuietly(inputStream); } } return mimeType; }
MimeTypeHelper { public static String getMimeType(byte[] fileData) { @SuppressWarnings("rawtypes") Collection mimeTypes = MIME_UTIL.getMimeTypes(fileData); String mimeType = MimeUtil2.getMostSpecificMimeType(mimeTypes).toString(); if (mimeType.startsWith("image/")) { try { Imaging.getImageInfo(fileData); return mimeType; } catch (ImageReadException e) { LOGGER.error("Imaging: Image seems to be invalid or unsupported", e); } catch (IOException e) { LOGGER.error("Imaging: There was an error reading the image", e); } ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData); try { ImageIO.read(inputStream).getAlphaRaster(); } catch (Exception e) { LOGGER.error("There was an error reading the image: {}", e); mimeType = "image/x-unknown"; } finally { IOUtils.closeQuietly(inputStream); } } return mimeType; } }
MimeTypeHelper { public static String getMimeType(byte[] fileData) { @SuppressWarnings("rawtypes") Collection mimeTypes = MIME_UTIL.getMimeTypes(fileData); String mimeType = MimeUtil2.getMostSpecificMimeType(mimeTypes).toString(); if (mimeType.startsWith("image/")) { try { Imaging.getImageInfo(fileData); return mimeType; } catch (ImageReadException e) { LOGGER.error("Imaging: Image seems to be invalid or unsupported", e); } catch (IOException e) { LOGGER.error("Imaging: There was an error reading the image", e); } ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData); try { ImageIO.read(inputStream).getAlphaRaster(); } catch (Exception e) { LOGGER.error("There was an error reading the image: {}", e); mimeType = "image/x-unknown"; } finally { IOUtils.closeQuietly(inputStream); } } return mimeType; } private  MimeTypeHelper(); }
MimeTypeHelper { public static String getMimeType(byte[] fileData) { @SuppressWarnings("rawtypes") Collection mimeTypes = MIME_UTIL.getMimeTypes(fileData); String mimeType = MimeUtil2.getMostSpecificMimeType(mimeTypes).toString(); if (mimeType.startsWith("image/")) { try { Imaging.getImageInfo(fileData); return mimeType; } catch (ImageReadException e) { LOGGER.error("Imaging: Image seems to be invalid or unsupported", e); } catch (IOException e) { LOGGER.error("Imaging: There was an error reading the image", e); } ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData); try { ImageIO.read(inputStream).getAlphaRaster(); } catch (Exception e) { LOGGER.error("There was an error reading the image: {}", e); mimeType = "image/x-unknown"; } finally { IOUtils.closeQuietly(inputStream); } } return mimeType; } private  MimeTypeHelper(); static String getMimeType(byte[] fileData); static String getMimeType(File file); static String getMimeType(InputStream fileStream, String filename); static String getMimeType(String absolutePathToFile); }
MimeTypeHelper { public static String getMimeType(byte[] fileData) { @SuppressWarnings("rawtypes") Collection mimeTypes = MIME_UTIL.getMimeTypes(fileData); String mimeType = MimeUtil2.getMostSpecificMimeType(mimeTypes).toString(); if (mimeType.startsWith("image/")) { try { Imaging.getImageInfo(fileData); return mimeType; } catch (ImageReadException e) { LOGGER.error("Imaging: Image seems to be invalid or unsupported", e); } catch (IOException e) { LOGGER.error("Imaging: There was an error reading the image", e); } ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData); try { ImageIO.read(inputStream).getAlphaRaster(); } catch (Exception e) { LOGGER.error("There was an error reading the image: {}", e); mimeType = "image/x-unknown"; } finally { IOUtils.closeQuietly(inputStream); } } return mimeType; } private  MimeTypeHelper(); static String getMimeType(byte[] fileData); static String getMimeType(File file); static String getMimeType(InputStream fileStream, String filename); static String getMimeType(String absolutePathToFile);  }