是否可以使用Tika从URL中提取文本?如果有任何链接,请提供。或者TIKA只能用于PDF、Word和其他媒体文件吗?
是否可以使用Tika从URL中提取文本?如果有任何链接,请提供。或者TIKA只能用于PDF、Word和其他媒体文件吗?
This is from lucid:
InputStream input = new FileInputStream(new File(resourceLocation));
ContentHandler textHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
PDFParser parser = new PDFParser();
parser.parse(input, textHandler, metadata);
input.close();
out.println("Title: " + metadata.get("title"));
out.println("Author: " + metadata.get("Author"));
out.println("content: " + textHandler.toString());
无需创建PDFParser
,您可以使用Tika的AutoDetectParser
自动处理不同类型的文件:
Parser parser = new AutoDetectParser();
是的,你可以这样做。以下是代码。此代码使用apache http客户端
HttpGet httpget = new HttpGet("http://url.here");
HttpEntity entity = null;
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpget);
entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();
parser.parse( instream, handler, metadata, new ParseContext());
String plainText = handler.toString();
FileWriter writer = new FileWriter( "/scratch/cache/output.txt");
writer.write( plainText );
writer.close();
System.out.println( "done");
}
如果要从URL中提取内容而不是本地文件,请使用以下代码:
byte[] raw = content.getContent();
ContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();
parser.parse(new ByteArrayInputStream(raw), handler, metadata, new ParseContext());
LOG.info("content: " + handler.toString());