当前位置: 首页>>代码示例>>Java>>正文


Java Detector.detect方法代码示例

本文整理汇总了Java中org.apache.tika.detect.Detector.detect方法的典型用法代码示例。如果您正苦于以下问题:Java Detector.detect方法的具体用法?Java Detector.detect怎么用?Java Detector.detect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.tika.detect.Detector的用法示例。


在下文中一共展示了Detector.detect方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: findMediaType

import org.apache.tika.detect.Detector; //导入方法依赖的package包/类
/**
 * Finds media type (through Apache Tika library), based on filename and magic numbers.
 * @throws IOException
 */
public static MediaType findMediaType(InputStream is, String fileName) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(is);
    try {
        AutoDetectParser parser = new AutoDetectParser();
        Detector detector = parser.getDetector();
        Metadata md = new Metadata();
        md.add(Metadata.RESOURCE_NAME_KEY, fileName);
        MediaType mediaType = detector.detect(bis, md);
        return mediaType;
    } finally {
        try {
            bis.close();
        } catch (IOException e) {
            ;
        }
    }
}
 
开发者ID:ilscipio,项目名称:scipio-erp,代码行数:22,代码来源:TikaUtil.java

示例2: isImage

import org.apache.tika.detect.Detector; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:reportportal,项目名称:client-java-core,代码行数:19,代码来源:ImageConverter.java

示例3: getMimeType

import org.apache.tika.detect.Detector; //导入方法依赖的package包/类
private static MediaType getMimeType(InputStream stream, Metadata md) throws IOException {
	MediaType mediaType;
	try (BufferedInputStream bis = new BufferedInputStream(stream)) {
		AutoDetectParser parser = new AutoDetectParser();
		Detector detector = parser.getDetector();
		mediaType = detector.detect(bis, md);
	}
	return mediaType;
}
 
开发者ID:U-QASAR,项目名称:u-qasar.platform,代码行数:10,代码来源:FileUploadUtil.java

示例4: getType

import org.apache.tika.detect.Detector; //导入方法依赖的package包/类
/**
 * Returns a data type.
 */
public final MediaType getType(final String alias)
		throws StorageIOException{
	try (InputStream in = this.getInputStream(alias)) {
	    AutoDetectParser parser = new AutoDetectParser();
	    Detector detector = parser.getDetector();
	    Metadata md = new Metadata();
	    md.add(Metadata.RESOURCE_NAME_KEY, alias);
	    return detector.detect(in, md);
	} catch (IOException exc) {
		throw new StorageIOException(alias, exc);
	}
}
 
开发者ID:SHAF-WORK,项目名称:shaf,代码行数:16,代码来源:AbstractStorageDriver.java

示例5: testExcelXLSB

import org.apache.tika.detect.Detector; //导入方法依赖的package包/类
/**
 * We don't currently support the .xlsb file format 
 *  (an OOXML container with binary blobs), but we 
 *  shouldn't break on these files either (TIKA-826)  
 */
@Test
public void testExcelXLSB() throws Exception {
   Detector detector = new DefaultDetector();
   AutoDetectParser parser = new AutoDetectParser();
   
   InputStream input = ExcelParserTest.class.getResourceAsStream(
         "/test-documents/testEXCEL.xlsb");
   Metadata m = new Metadata();
   m.add(Metadata.RESOURCE_NAME_KEY, "excel.xlsb");
   
   // Should be detected correctly
   MediaType type = null;
   try {
      type = detector.detect(input, m);
      assertEquals("application/vnd.ms-excel.sheet.binary.macroenabled.12", type.toString());
   } finally {
      input.close();
   }
   
   // OfficeParser won't handle it
   assertEquals(false, (new OfficeParser()).getSupportedTypes(new ParseContext()).contains(type));
   
   // OOXMLParser won't handle it
   assertEquals(false, (new OOXMLParser()).getSupportedTypes(new ParseContext()).contains(type));
   
   // AutoDetectParser doesn't break on it
   input = ExcelParserTest.class.getResourceAsStream("/test-documents/testEXCEL.xlsb");

   try {
      ContentHandler handler = new BodyContentHandler(-1);
      ParseContext context = new ParseContext();
      context.set(Locale.class, Locale.US);
      parser.parse(input, handler, m, context);

      String content = handler.toString();
      assertEquals("", content);
   } finally {
      input.close();
   }
}
 
开发者ID:kanrourou,项目名称:software-testing,代码行数:46,代码来源:ExcelParserTest.java

示例6: testExcel95

import org.apache.tika.detect.Detector; //导入方法依赖的package包/类
/**
 * We don't currently support the old Excel 95 .xls file format, 
 *  but we shouldn't break on these files either (TIKA-976)  
 */
@Test
public void testExcel95() throws Exception {
   Detector detector = new DefaultDetector();
   AutoDetectParser parser = new AutoDetectParser();
   
   InputStream input = ExcelParserTest.class.getResourceAsStream(
         "/test-documents/testEXCEL_95.xls");
   Metadata m = new Metadata();
   m.add(Metadata.RESOURCE_NAME_KEY, "excel_95.xls");
   
   // Should be detected correctly
   MediaType type = null;
   try {
      type = detector.detect(input, m);
      assertEquals("application/vnd.ms-excel", type.toString());
   } finally {
      input.close();
   }
   
   // OfficeParser will claim to handle it
   assertEquals(true, (new OfficeParser()).getSupportedTypes(new ParseContext()).contains(type));
   
   // OOXMLParser won't handle it
   assertEquals(false, (new OOXMLParser()).getSupportedTypes(new ParseContext()).contains(type));
   
   // AutoDetectParser doesn't break on it
   input = ExcelParserTest.class.getResourceAsStream("/test-documents/testEXCEL_95.xls");

   try {
      ContentHandler handler = new BodyContentHandler(-1);
      ParseContext context = new ParseContext();
      context.set(Locale.class, Locale.US);
      parser.parse(input, handler, m, context);

      String content = handler.toString();
      assertEquals("", content);
   } finally {
      input.close();
   }
}
 
开发者ID:kanrourou,项目名称:software-testing,代码行数:45,代码来源:ExcelParserTest.java


注:本文中的org.apache.tika.detect.Detector.detect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。