代码语言:javascript复制
package com.mengyao.tika.app;
import java.io.File;
import java.io.FileInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.jpeg.JpegParser;
import org.apache.tika.sax.BodyContentHandler;
public class JpegApp {
public static void main(final String[] args) throws Exception {
// Tika默认是10*1024*1024,这里防止文件过大导致Tika报错
BodyContentHandler handler = new BodyContentHandler(1024 * 1024 * 10);
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(new File("D:/无标题.jpg"));
ParseContext pcontext = new ParseContext();
// 解析JPEG文件时应由超类AbstractParser的派生类JpegParser实现
JpegParser msofficeparser = new JpegParser();
msofficeparser.parse(inputstream, handler, metadata, pcontext);
// 获取JPEG文件的内容
System.out.println("JPEG文件内容:" handler.toString());
// 获取JPEG文件的元数据
System.out.println("JPEG文件元数据:");
String[] metadataNames = metadata.names();
for (String name : metadataNames) {
System.out.println(name " : " metadata.get(name));
}
}
}
代码语言:javascript复制方法2:
代码语言:javascript复制public String tikaTool(File f){
Tika tika=new Tika();
try
{
return tika.parseToString(f);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (TikaException e)
{
e.printStackTrace();
}
return null;
}