1.4 TIKA 兼容JDK1.6 获取流,元数据,内容

1 篇文章 0 订阅
1 篇文章 0 订阅

兼容JDK1.6的最高版本的是 1.4版本的TIKA

1.4版本的TIKA,在读取TXT内容时候存在一些问题,有更好的解决办法,可以在下方留言。

以下为doc、docx、ppt、pptx、xls、xlsx,wps,ptf,rtf,htm。txtl格式

获取流,元数据,内容方式(笔者亲测过)

1)获取BufferedReader 流

public BufferedReader getReader(File file) throws Exception
{


BufferedReader reader = null;
String extension = FilenameUtils.getExtension(file.getName());
if ("txt".equals(extension))
{
FileInputStream fis = new FileInputStream(file);
AutoDetectReader dr = new AutoDetectReader(fis);
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), dr.getCharset()));
fis.close();
}
else
{
reader = new BufferedReader(new Tika().parse(file));
}
return reader;
}

2)获取内容(txt内容读取,用apache的common.io包实现)

public String getContent(File file) throws Exception
{
String content = null;
String extension = FilenameUtils.getExtension(file.getName());
if ("txt".equals(extension))
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
AutoDetectReader dr = new AutoDetectReader(fis);
content = FileUtils
.readFileToString(file, dr.getCharset());
}
catch (Exception e)
{
throw e;
}
finally
{
if (fis != null)
fis.close();
}
}
else
{
content = new Tika().parseToString(file);
}
return content;
}

3)获取元数据

public Metadata getMetadata(File file) throws Exception
{
FileInputStream fis = new FileInputStream(file);
Metadata metadata = new Metadata();
Tika tika = new Tika();
tika.setMaxStringLength(0);
tika.parseToString(fis, metadata, 0);
return metadata;
}

数据的进一步封装:

Map<String, String> metaMap = new HashMap<String, String>();
Metadata metadata = getMetadata();


for (String name : metadata.names())
{
metaMap.put(name, metadata.get(name));
}

性能方面还未进行详细测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值