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.     }  
/**
	 * 解析各种类型文件
	 * @param 文件路径
	 * @return 文件内容字符串
	 */
	public static String parse(String path) {
		String result = "";
		TikaConfig tikaConfig = TikaConfig.getDefaultConfig();
		try {
			result = ParseUtils.getStringContent(new File(path), tikaConfig);
		}catch (Exception e) {
			log.debug("[by ninja.hzw]" + e);
		}
		return result;
	}

很简单,可以解析各种文件,返回文档内容字符串, 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  
To build Tika from sources you first need to either download a source release or checkout the latest sources from version control.

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.

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/target/tika-core-0.7.jar
Tika core library. Contains the core interfaces and classes of Tika, but none of the parser implementations. Depends only on Java 5.
tika-parsers/target/tika-parsers-0.7.jar
Tika parsers. Collection of classes that implement the Tika Parser interface based on various external parser libraries.
tika-app/target/tika-app-0.7.jar
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.
tika-bundle/target/tika-bundle-0.7.jar
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>  
  <dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>0.7</version>
  </dependency>

以及

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

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

转载: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.     }  
/**
	 * 解析各种类型文件
	 * @param 文件路径
	 * @return 文件内容字符串
	 */
	public static String parse(String path) {
		String result = "";
		TikaConfig tikaConfig = TikaConfig.getDefaultConfig();
		try {
			result = ParseUtils.getStringContent(new File(path), tikaConfig);
		}catch (Exception e) {
			log.debug("[by ninja.hzw]" + e);
		}
		return result;
	}

很简单,可以解析各种文件,返回文档内容字符串, 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  
To build Tika from sources you first need to either download a source release or checkout the latest sources from version control.

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.

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/target/tika-core-0.7.jar
Tika core library. Contains the core interfaces and classes of Tika, but none of the parser implementations. Depends only on Java 5.
tika-parsers/target/tika-parsers-0.7.jar
Tika parsers. Collection of classes that implement the Tika Parser interface based on various external parser libraries.
tika-app/target/tika-app-0.7.jar
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.
tika-bundle/target/tika-bundle-0.7.jar
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>  
  <dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>0.7</version>
  </dependency>

以及

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

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

猜你喜欢

转载自shuang7255.iteye.com/blog/1717932