Apache Tika - 检测 JSON / PDF 特定的 MIME 类型
Apache Tika - detect JSON / PDF specific mime type
我正在使用 Apache Tika 从其 base64 表示中检测文件 Mime 类型。
很遗憾,我没有关于该文件的其他信息(例如扩展名)。
我可以做些什么来使 Tika 更具体吗?
我目前正在使用这个:
Tika tika = new Tika();
tika.setMaxStringLength(-1);
String mimetype = tika.detect(Base64.decode(fileString));
它为我提供 JSON 和 PDF 文件的 text/plain
,但我想获得更具体的信息:application/json
、application/pdf
等...
希望有人能帮助我!
谢谢。
在我过去的项目中我使用了TikaConfig
我做的是:
//Note you can use alse byte[] instead of InputStream
InputStream is = new FileInputStream(new File(YOUR_FILE));
TikaConfig tc = new TikaConfig();
Metadata md = new Metadata();
md.set(Metadata.RESOURCE_NAME_KEY, fileName);
String mimeType = tc.getDetector().detect(TikaInputStream.get(is), md).toString();
通过使用 byte[]
:
byte[] fileBytes = GET_BYTE_ARRAY_FROM_YOUR_FILE;
TikaConfig tc = new TikaConfig();
Metadata md = new Metadata();
md.set(Metadata.RESOURCE_NAME_KEY, fileName);
String mimeType = tc.getDetector().detect(TikaInputStream.get(fileBytes), md).toString();
我在获得正确的 mimeType 方面没有问题....
希望有用
安杰洛
Tika#detect(String)
Detects the media type of a document with the given file name.
传递 PDF 或 JSON 文件的内容不会像 this method expects a filename 那样工作。 Tika 将回退到 text/plain
,因为它找不到任何匹配的文件名。
PDF
对于 PDF,您只需要将一些数据写入流,或者将一些字节传递给它,然后让 Tika 使用 Mime Magic Detection 通过查找特殊的 ("magic" ) 文件开头附近的字节模式(在纯文本中为 %PDF
):
String pdfContent = "%PDF-1.4\n%\E2\E3\CF\D3"; // i.e. base64 decoded
Tika tika = new Tika();
System.out.println(tika.detect(pdfContent.getBytes())); // "application/pdf"
JSON
但是对于 JSON,即使是这种方法也会 return text/plain
& Tika 是正确的。 application/json
就像纯文本的子类型,表示应该对文本进行不同的解释。所以如果你得到 text/plain
,这就是你必须做的。使用 JSON 库(例如 Jackson)解析内容以查看其是否有效 JSON:
Sring json = "[1, 2, 3]"; // an array in JSON
try {
final JsonParser parser = new ObjectMapper().getFactory().createParser(json);
while (parser.nextToken() != null) {
}
System.out.println("Probably JSON!");
} catch (Exception e) {
System.out.println("Definitely not JSON!");
}
请注意您想要的严格程度,因为 Jackson 将单个数字 1
视为有效 JSON 但事实并非如此。为了解决这个问题,您可以首先测试字符串是否以 {
或 [
开头(可能前面有空格),甚至在尝试将其解析为 JSON.
我正在使用 Apache Tika 从其 base64 表示中检测文件 Mime 类型。 很遗憾,我没有关于该文件的其他信息(例如扩展名)。
我可以做些什么来使 Tika 更具体吗?
我目前正在使用这个:
Tika tika = new Tika();
tika.setMaxStringLength(-1);
String mimetype = tika.detect(Base64.decode(fileString));
它为我提供 JSON 和 PDF 文件的 text/plain
,但我想获得更具体的信息:application/json
、application/pdf
等...
希望有人能帮助我!
谢谢。
在我过去的项目中我使用了TikaConfig
我做的是:
//Note you can use alse byte[] instead of InputStream
InputStream is = new FileInputStream(new File(YOUR_FILE));
TikaConfig tc = new TikaConfig();
Metadata md = new Metadata();
md.set(Metadata.RESOURCE_NAME_KEY, fileName);
String mimeType = tc.getDetector().detect(TikaInputStream.get(is), md).toString();
通过使用 byte[]
:
byte[] fileBytes = GET_BYTE_ARRAY_FROM_YOUR_FILE;
TikaConfig tc = new TikaConfig();
Metadata md = new Metadata();
md.set(Metadata.RESOURCE_NAME_KEY, fileName);
String mimeType = tc.getDetector().detect(TikaInputStream.get(fileBytes), md).toString();
我在获得正确的 mimeType 方面没有问题....
希望有用
安杰洛
Tika#detect(String)
Detects the media type of a document with the given file name.
传递 PDF 或 JSON 文件的内容不会像 this method expects a filename 那样工作。 Tika 将回退到 text/plain
,因为它找不到任何匹配的文件名。
对于 PDF,您只需要将一些数据写入流,或者将一些字节传递给它,然后让 Tika 使用 Mime Magic Detection 通过查找特殊的 ("magic" ) 文件开头附近的字节模式(在纯文本中为 %PDF
):
String pdfContent = "%PDF-1.4\n%\E2\E3\CF\D3"; // i.e. base64 decoded
Tika tika = new Tika();
System.out.println(tika.detect(pdfContent.getBytes())); // "application/pdf"
JSON
但是对于 JSON,即使是这种方法也会 return text/plain
& Tika 是正确的。 application/json
就像纯文本的子类型,表示应该对文本进行不同的解释。所以如果你得到 text/plain
,这就是你必须做的。使用 JSON 库(例如 Jackson)解析内容以查看其是否有效 JSON:
Sring json = "[1, 2, 3]"; // an array in JSON
try {
final JsonParser parser = new ObjectMapper().getFactory().createParser(json);
while (parser.nextToken() != null) {
}
System.out.println("Probably JSON!");
} catch (Exception e) {
System.out.println("Definitely not JSON!");
}
请注意您想要的严格程度,因为 Jackson 将单个数字 1
视为有效 JSON 但事实并非如此。为了解决这个问题,您可以首先测试字符串是否以 {
或 [
开头(可能前面有空格),甚至在尝试将其解析为 JSON.