Apache tika -- 解析多种类型(word、pdf、txt 等)文件!

http://cloudera.iteye.com/blog/737629

apache 是个伟大的组织。

在lucene 检索 如火如荼时, apache不忘继续努力,近期提供了对各种格式文件进行解析的解决方案 -- apache旗下的tika. 虽然还没有1.0版  , 但已经很好用:

Java代码   收藏代码
  1. /** 
  2.      * 解析各种类型文件 
  3.      * @param 文件路径 
  4.      * @return 文件内容字符串 
  5.      */  
  6.     public static String parse(String path) {  
  7.         String result = "";  
  8.         TikaConfig tikaConfig = TikaConfig.getDefaultConfig();  
  9.         try {  
  10.             result = ParseUtils.getStringContent(new File(path), tikaConfig);  
  11.         }catch (Exception e) {  
  12.             log.debug("[by ninja.hzw]" + e);  
  13.         }  
  14.         return result;  
  15.     }  

 

很简单,可以解析各种文件,返回文档内容字符串, word2003/2007 、 pdf  、 txt 都经过测试,均能解析且无乱码问题。  

 

oh, Great Apach

 

Tika 的下载和打包:

下载不用多说,google 一下“apache tika” 找到其官网下载即可。

Java代码   收藏代码
  1. To build Tika from sources you first need to either download a source release or checkout the latest sources from version control.  
  2.   
  3. Once you have the sources, you can build them using the Maven 2 build system. Executing the following command in the base directory will build the sources and install the resulting artifacts in your local Maven repository.  
  4.   
  5. mvn install  

 

apache 已经说得很清楚,进入下载后的tika 目录 ,然后执行maven install 即可。(当然这里需要您懂得maven2的使用。当然不会的朋友可以联系我^^ . 还需注意,必须为jdk1.5 + 才能成功编译打包。)

打包完后产生以下 jar:

Java代码   收藏代码
  1. tika-core/target/tika-core-0.7.jar  
  2. Tika core library. Contains the core interfaces and classes of Tika, but none of the parser implementations. Depends only on Java 5.  
  3. tika-parsers/target/tika-parsers-0.7.jar  
  4. Tika parsers. Collection of classes that implement the Tika Parser interface based on various external parser libraries.  
  5. tika-app/target/tika-app-0.7.jar  
  6. Tika application. Combines the above libraries and all the external parser libraries into a single runnable jar with a GUI and a command line interface.  
  7. tika-bundle/target/tika-bundle-0.7.jar  
  8. Tika bundle. An OSGi bundle that includes everything you need to use all Tika functionality in an OSGi environment.  

 

 我们要想做文档解析,只需引入tika-core 和 tika-parsers 即可。

 

当然如果您的项目是maven 构建的,那更好了。在pom里加上依赖:

Java代码   收藏代码
  1. <dependency>  
  2.   <groupId>org.apache.tika</groupId>  
  3.   <artifactId>tika-core</artifactId>  
  4.   <version>0.7</version>  
  5. </dependency>  

 

以及

Java代码   收藏代码
  1. <dependency>  
  2.   <groupId>org.apache.tika</groupId>  
  3.   <artifactId>tika-parsers</artifactId>  
  4.   <version>0.7</version>  
  5. </dependency>  

 

maven 会自动下载。(感谢maven官方的支持。)


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值