【问题标题】:how to judge if the file is doc or docx in POIPOI中如何判断文件是doc还是docx
【发布时间】:2017-11-25 05:46:36
【问题描述】:

标题可能有点混乱。最简单的方法一定是通过扩展名来判断,如下:

// is represents the InputStream   
if (filePath.endsWith("doc")) {
    WordExtractor ex = new WordExtractor(is);
    text = ex.getText();
    ex.close();
} else if(filePath.endsWith("docx")) {
    XWPFDocument doc = new XWPFDocument(is);
    XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
    text = extractor.getText();
    extractor.close();
}

这在大多数情况下都有效。但是我发现对于某些扩展名为doc(本质上是docx 文件)的文件,如果你使用winrar 打开,你会发现xml 文件。众所周知,docx 文件是由xml 文件组成的zip 文件。 我相信这个问题一定不会少见。但我还没有找到任何有关此的信息。显然,从扩展名来看docdocx是不合适的。

就我而言,我必须阅读很多文件。我什至会在压缩文件中读取docdocxzip7z 甚至rar。因此,我必须通过 inputStream 而不是 File 或其他东西来读取内容。所以how to know whether a file is .docx or .doc format from Apache POI 完全不适合我ZipInputStream 的情况。

判断文件的最佳方法是doc 还是docx?我想要一个解决方案来从可能是docdocx 的文件中读取内容。但不仅仅是简单地判断它是doc还是docx。显然,ZipInpuStream 对我来说不是一个好方法。而且我认为这对其他人也不是合适的方法。为什么一定要通过异常判断文件是doc还是docx

【问题讨论】:

  • @ClayFerguson 请仔细阅读我的问题,我已经看到了。我想获得一种适当的方式来读取 doc 或 docx 文件。
  • 我也不知道@ClayFerguson 的链接如何没有回答您的问题。引用的解决方案提供了一种简单的方法来测试文件是否为 Zip 文件...从而区分 doc 和 docx。
  • @neal,所以一旦您检测到它是一个 zip 文件,您仍然会尝试将其视为“doc”文件吗?是的,这会“带来问题”。

标签: java ms-word apache-poi


【解决方案1】:

使用当前稳定的apache poi 3.17 版,您可以使用FileMagic。但是internally 当然也会查看查看文件。

例子:

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;

import org.apache.poi.poifs.filesystem.FileMagic;

import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class ReadWord {

 static String read(InputStream is) throws Exception {

System.out.println(FileMagic.valueOf(is));

  String text = "";

  if (FileMagic.valueOf(is) == FileMagic.OLE2) {
   WordExtractor ex = new WordExtractor(is);
   text = ex.getText();
   ex.close();
  } else if(FileMagic.valueOf(is) == FileMagic.OOXML) {
   XWPFDocument doc = new XWPFDocument(is);
   XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
   text = extractor.getText();
   extractor.close();
  }

  return text;

 }

 public static void main(String[] args) throws Exception {

  InputStream is = new BufferedInputStream(new FileInputStream("ExampleOLE.doc")); //really a binary OLE2 Word file
  System.out.println(read(is));
  is.close();

  is = new BufferedInputStream(new FileInputStream("ExampleOOXML.doc")); //a OOXML Word file named *.doc
  System.out.println(read(is));
  is.close();

  is = new BufferedInputStream(new FileInputStream("ExampleOOXML.docx")); //really a OOXML Word file
  System.out.println(read(is));
  is.close();

 }
}

【讨论】:

  • 非常感谢!!终于有一个很棒的解决方案了。我会尝试阅读这个的实现。
【解决方案2】:
try {
    new ZipFile(new File("/Users/giang/Documents/a.doc"));
    System.out.println("this file is .docx");
} catch (ZipException e) {
    System.out.println("this file is not .docx");
    e.printStackTrace();
}

【讨论】:

  • 在 cmets 中透露了一些关于此响应未能满足的额外需求。
猜你喜欢
  • 2011-09-12
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多