代码片段,使用TIKA来解析PDF,WORD和EMAIL

/**
 * com.jiaoyiping.pdstest.TestTika.java
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.jiaoyiping.pdstest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.mail.RFC822Parser;
import org.apache.tika.parser.microsoft.OfficeParser;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.sax.BodyContentHandler;
import org.junit.Test;
import org.xml.sax.ContentHandler;

/**
 * <pre>
 * Desc:
 * @author 焦一平
 * @refactor 焦一平
 * @date   2014年12月4日 下午1:31:09
 * @version 1.0
 * @see
 * REVISIONS:
 * Version 	   Date 		    Author 			  Description
 * -------------------------------------------------------------------
 * 1.0 		  2014年12月4日 	                              焦一平  	         1. Created this class.
 * </pre>
 */
public class TestTika {

	//解析PDF
	@Test
	public void testPdf() throws Exception{
		Long start = System.currentTimeMillis();
		Parser parser = new PDFParser();
		InputStream is = new BufferedInputStream(new FileInputStream(new File("D:\\我的微盘\\文档\\参考文档\\Linux Shell脚本攻略.pdf")));
		OutputStream os = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\result.txt")));
	    Metadata meta = new Metadata();
	    meta.add(Metadata.CONTENT_ENCODING, "utf-8");
        ContentHandler iHandler = new BodyContentHandler(os);
	    parser.parse(is, iHandler, meta, new ParseContext());
	    Long end = System.currentTimeMillis();
	    Long used = (end-start)/1000;
	    System.out.println("耗时: "+used+"秒");
	}
	//解析Word
	@Test
	public void testWrod() throws Exception{
		Long start = System.currentTimeMillis();
		Parser parser = new OfficeParser();
		InputStream is = new BufferedInputStream(new FileInputStream(new File("D:\\我的微盘\\文档\\参考文档\\jBPM5_用户指南中文版.doc")));
		OutputStream os = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\result.txt")));
		Metadata meta = new Metadata();
	    meta.add(Metadata.CONTENT_ENCODING, "utf-8");
        ContentHandler iHandler = new BodyContentHandler(os);
	    parser.parse(is, iHandler, meta, new ParseContext());

		Long end = System.currentTimeMillis();
		Long used = (end-start)/1000;
		System.out.println("耗时:"+used+"秒");
	}
	//解析EMAIL(只能解析标准的eml格式的,不能解析微软的msg格式)
	//使用commons-email来进行解析的可以得到收件人、发件人、主题、内容等元数据,TIkA是否支持未尝试
	@Test
	public void testEmail() throws Exception{
		Long start = System.currentTimeMillis();
		Parser parser = new RFC822Parser();
		InputStream is = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\Administrator\\Downloads\\回复_ RE_ 数据导入工作 - 外部系统枚举与U-Cloud枚举映射.eml")));
		OutputStream os = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\result.txt")));
		Metadata meta = new Metadata();
		meta.add(Metadata.CONTENT_ENCODING, "utf-8");
		ContentHandler iHandler = new BodyContentHandler(os);
		parser.parse(is, iHandler, meta, new ParseContext());

		Long end = System.currentTimeMillis();
		Long used = (end-start)/1000;
		System.out.println("耗时:"+used+"秒");
	}
}

  

时间: 2024-08-02 02:50:23

代码片段,使用TIKA来解析PDF,WORD和EMAIL的相关文章

Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑",如果是两个字那就是"巨坑"=>因为这个需求一开始并不是这样子的,且听我漫漫道来: 一开始客户与我们商量的是将office和PDF上传,将此类文件解析成html格式,在APP端调用内置server直接以html"播放" 经历一个月~,两个月~,三个月~~~

【转】46 个非常有用的 PHP 代码片段

1. 发送 SMS 在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息.下面的 PHP 代码就实现了发送 SMS 的功能. 为了使用任何的语言发送 SMS,需要一个 SMS gateway.大部分的 SMS 会提供一个 API,这里是使用 MSG91 作为 SMS gateway. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

46 个非常有用的 PHP 代码片段

http://www.oschina.net/question/2012764_246023?from=20150809 1. 发送 SMS 在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息.下面的 PHP 代码就实现了发送 SMS 的功能. 为了使用任何的语言发送 SMS,需要一个 SMS gateway.大部分的 SMS 会提供一个 API,这里是使用MSG91 作为 SMS gateway. 1 2 3 4 5 6 7 8 9 1

PHP必用代码片段

2018-10-30 在编写代码的时候有个神奇的工具总是好的!下面这里收集了 50+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常有帮助,非常容易学习,让我们开始学习吧- 1. 发送 SMS 在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息.下面的 PHP 代码就实现了发送 SMS 的功能. 为了使用任何的语言发送 SMS,需要一个 SMS gateway.大部分的 SMS 会提供一个

python解析PDF文档

1.安装 pip install pdfminer3k 2.  python读取PDF文档代码分析 PDF格式不是规范格式. 尽管它被叫做"PDF文档", 但并不像word或者html文档.PDF的表现更像一张图片.PDF更像是在一张纸的各个准确的位置上把内容都摆放出来.大部分情况下,没有逻辑结构,比如句子或段落,并且不能自适应页面大小的调整.PDFMiner尝试通过猜测它们的布局来重建它们的结构,但是不保证一定能工作.我知道这样很难看,但是,PDF确实不够规范. 下面这个图片是使用流

100个直接可以拿来用的JavaScript实用功能代码片段

把平时网站上常用的一些实用功能代码片段通通收集起来,方面网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作效率. 目录如下: 1.原生JavaScript实现字符串长度截取2.原生JavaScript获取域名主机3.原生JavaScript清除空格4.原生JavaScript替换全部5.原生JavaScript转义html标签6.原生JavaScript还原html标签7.原生JavaScript时间日期格式转换8.原生JavaScript判断是否为数字类型9.原生JavaScript

收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发.

1. 禁止右键点击 $(document).ready(function(){     $(document).bind("contextmenu",function(e){         return false;     }); }); 2. 隐藏搜索文本框文字 Hide when clicked in the search field, the value.(example can be found below in the comment fields) $(document

30+有用的CSS代码片段

在一篇文章中收集所有的CSS代码片段几乎是不可能的事情,但是我们这里列出了一些相对于其他的更有用的代码片段,不要被这些代码的长度所吓到,因为它们都很容易实现,并且具有良好的文档.除了那些解决常见的恼人的问题外,也包含了一些解决新问题的新技术. 1. 垂直对齐 如果你之前遇到过这个问题,你就应该知道它是多么的烦人,幸运的是,现在你可以使用CSS3变换来解决这个问题: .vc{ position: relative; top: 50%; -webkit-transform: translateY(-

一些实用的JQuery代码片段收集

本文将展示50个非常实用的JQuery代码片段,这些代码能够给你的JavaScript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助你又快又好地把事情完成.好好仔细看一遍,你肯定会有收获.嫩江县海洋局 如何修改jQuery默认编码(例如默认UTF-8改成改GB2312): $.ajaxSetup({ ajaxSettings:{ contentType:"application/x-www-form-urlencoded;c