ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] Apache Tika 아파치 티카
    Back/JAVA 2020. 8. 12. 14:33

    :: PPT, CSV ,PDF 등 다양한 형태의, 파일의 메타 데이터와 텍스트를 감지하고 추출하는 라이브러리

     

     - CSV의 인코딩 타입을 확인할 목적으로 사용했다

     - tika 외 메타데이터 추출을 돕는 라이브러리가 있으나 최근까지 release 하길래 선택했다

     - juniversalchardet , ICU4J

     

    :: tika-core - 핵심 라이브러리 (파서 없음)

    :: tika-parsers - core + Tika Parser interface


    <dependency>
    	<groupId>org.apache.tika</groupId>
    	<artifactId>tika-parsers</artifactId>
    	<version>1.24.1</version>
    </dependency>

     # metadata

    	public static void getMetadata() {
    		
    		Tika tika = new Tika();
    		Metadata metadata = new Metadata();
    
    		try (TikaInputStream reader = TikaInputStream.get(Paths.get(DEFAULTPATH))){
    			
    			// 파일 본문
    			String contents = tika.parseToString(reader, metadata);
    
    			/*
    			 * 파일 메타데이터 X-Parsed-By: org.apache.tika.parser.DefaultParser
    			 * Content-Encoding: UTF-8
    			 * csv:delimiter: comma
    			 * Content-Type: text/csv; charset=UTF-8; delimiter=comma
    			 */
                 
    	        for(String name : metadata.names()) {
    	            System.out.println(name + ": " + metadata.get(name));
    	        }
    			
    		} catch (IOException | TikaException e) {
    			e.printStackTrace();
    		}
    		
    	}

     

    # mime type

    	public static void getMimeType(File file) {
    		String mimeType;
    		Tika tika = new Tika();
    		
    		try {
    			mimeType = tika.detect(file);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}

     

    # charset 

    	public static void getCharset() {
    		try {
    			byte[] arr = Files.readAllBytes(Paths.get(DEFAULTPATH));
    			
    			CharsetDetector charsetDetector = new CharsetDetector();
    			charsetDetector.setText(arr);
    			charsetDetector.enableInputFilter(true);
    			CharsetMatch cm = charsetDetector.detect();
    			
    			System.out.println(cm.getName());
    			
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}

     

    ** https://www.tutorialspoint.com/tika/index.htm

    ** https://tika.apache.org/

    ** https://github.com/apache/tika

     

     

     

     

    'Back > JAVA' 카테고리의 다른 글

    [JAVA] API 호출 - HttpURLConnection  (0) 2020.07.28
    [JAVA] JAVAMail API  (0) 2020.06.15
    [Serializable - 자바 직렬화]  (0) 2020.02.28
Designed by Tistory.