public static Detector getDetector(Parser p) { AutoDetectParser adp = getAutoDetectParser(p); return adp.getDetector(); }
try (InputStream is = theInputStream; BufferedInputStream bis = new BufferedInputStream(is);) { AutoDetectParser parser = new AutoDetectParser(); Detector detector = parser.getDetector(); Metadata md = new Metadata(); md.add(Metadata.RESOURCE_NAME_KEY, theFileName); MediaType mediaType = detector.detect(bis, md); return mediaType.toString(); }
InputStream is = MainApp.class.getResourceAsStream("/NetFx20SP1_x64.txt"); BufferedInputStream bis = new BufferedInputStream(is); AutoDetectParser parser = new AutoDetectParser(); Detector detector = parser.getDetector(); Metadata md = new Metadata(); md.add(Metadata.RESOURCE_NAME_KEY,MainApp.class.getResource("/NetFx20SP1_x64.txt").getPath()); MediaType mediaType = detector.detect(bis, md); System.out.println("MIMe Type of File : " + mediaType.toString());
private static void fillMetadata(AutoDetectParser parser, Metadata metadata, String contentType, String uri) { fillMetadata(metadata, contentType, uri); final Detector detector = parser.getDetector(); parser.setDetector(new Detector() { public MediaType detect(InputStream inputStream, Metadata metadata) throws IOException { String contentType = metadata.get(HttpHeaders.CONTENT_TYPE); if (contentType != null) { return MediaType.parse(contentType); } else { return detector.detect(inputStream, metadata); } } }); }
/** * Creates a new Tika-based parser. * @param parser Tika parser */ public AbstractTikaParser(Parser parser) { super(); this.parser = parser; if (parser instanceof AutoDetectParser) { AutoDetectParser p = (AutoDetectParser) parser; knownDetector = new ThreadSafeCacheableAutoDetectWrapper(p.getDetector()); p.setDetector(knownDetector); } else { knownDetector = null; } }
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); }
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); }
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); }
public String detectContentType(byte[] bytes, String filename) { InputStream bais = new ByteArrayInputStream(bytes); InputStream bis = new BufferedInputStream(bais); try { AutoDetectParser parser = new AutoDetectParser(); Detector detector = parser.getDetector(); Metadata md = new Metadata(); if (filename != null) { md.add(Metadata.RESOURCE_NAME_KEY, filename); } MediaType mediaType = detector.detect(bis, md); return mediaType.toString(); } catch (Exception ex) { ex.printStackTrace(); } finally { try { bais.close(); bis.close(); } catch (Exception ex2) { ex2.printStackTrace(); } } return null; }
/** * Check is input file is image * * @param fileContent */ public static boolean isImage(byte[] fileContent) { AutoDetectParser parser = new AutoDetectParser(); Detector detector = parser.getDetector(); MediaType mediaType; try { mediaType = detector.detect(TikaInputStream.get(fileContent), new Metadata()); } catch (Exception e) { logger.error("Unable to read file content.", e); throw new InternalReportPortalClientException("Unable to read file content.", e); } return mediaType.toString().contains(IMAGE_TYPE); }
/** * Detect the mime type of the stream. The stream is marked at the beginning and reset at the end * * @param bis the stream * @param fn the fileName of the file belonging to the stream */ public static MediaType getMimeType(BufferedInputStream bis, String fn) throws IOException { AutoDetectParser parser = new AutoDetectParser(); Detector detector = parser.getDetector(); Metadata md = new Metadata(); md.add(Metadata.RESOURCE_NAME_KEY, fn); final MediaType mediaType = detector.detect(bis, md); return mediaType; }
InputStream is = new BufferedInputStream(new FileInputStream(file)); AutoDetectParser parser = new AutoDetectParser(); Detector detector = parser.getDetector(); Metadata md = new Metadata(); md.add(Metadata.RESOURCE_NAME_KEY, "test.msg");
MediaType mt = ap.getDetector().detect(TikaInputStream.get(file), metadata); String mimetype = mt.toString();
MediaType mt = ap.getDetector().detect(TikaInputStream.get(file), metadata); String mimetype = mt.toString();