概述
引入mavenjar包(提取不通格式文件内容 需要引入相关依赖的包)
<!--tika-->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.26</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.26</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jbig2-imageio</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-jpeg2000</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
代码
public static void main(String[] args) {
String filePath="D:\tools\nginx-1.18.0\html\index.html";
Tika tika = new Tika();
try {
//System.out.println("---"+tika.parseToString(new File(filePath)));
//推荐使用
System.out.println("--1-"+new TikaController().fileToTxt(new File(filePath))+"---e1-");
System.out.println("--2-"+new TikaController().tikaTool(new File(filePath))+"---e2-");
}catch (Exception e) {
e.printStackTrace();
}
}
public String tikaTool(File f) throws IOException, TikaException {
Tika tika = new Tika();
Metadata metadata = new Metadata();
metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
String str = tika.parseToString(new FileInputStream(f), metadata);
for(String name : metadata.names()) {
//System.out.println(name+":"+metadata.get(name));
}
//return tika.parseToString(f);
return str;
}
public String fileToTxt(File f) {
Parser parser = new AutoDetectParser();
InputStream is = null;
try {
Metadata metadata = new Metadata();
metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
is = new FileInputStream(f);
ContentHandler handler = new BodyContentHandler();
ParseContext context = new ParseContext();
context.set(Parser.class, parser);
parser.parse(is, handler, metadata, context);
for (String name : metadata.names()) {
//System.out.println(name +":"+metadata.get(name));
}
return handler.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
最后
以上就是内向小蜜蜂为你收集整理的tika提取文件内容的全部内容,希望文章能够帮你解决tika提取文件内容所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复