图片文件读取报错!! 5C

文件number.doc原数据
图片说明
代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class Test {
public String readWord(String path) {
String buffer = "";

try {
if (path.endsWith(".doc")) {
InputStream is = new FileInputStream(new File(path));
WordExtractor ex = new WordExtractor(is);
buffer = ex.getText();
ex.close();
} else {
System.out.println("请选择后缀为.doc的文件");
}

} catch (Exception e) {
e.printStackTrace();
}
return buffer;
}
public static void main(String[] args) {
Test tp = new Test();
String content = tp.readWord("D:\number.doc"); //文件存放的地址
System.out.println("content===="+content);
}
}

myeclipse编译时信息
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:131)
at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:104)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:128)
at org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:106)
at org.apache.poi.hwpf.extractor.WordExtractor.(WordExtractor.java:53)
at Test.readWord(Test.java:14)
at Test.main(Test.java:28)
content====

有知道原因的吗?

3个回答

用的api不对,错误信息里面已经告诉你了 (XSSF instead of HSSF),新版的office格式xlsx,需要新的那套api

不同版本excel对应POI版不同,原因和解决办法参考这篇文章:POI - 读取Excel2003、Excel2007或更高级的兼容性问题

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class Test {
public String readWord(String path) {
String buffer = "";

try {
if (path.endsWith(".doc")) {
InputStream is = new FileInputStream(new File(path));
WordExtractor ex = new WordExtractor(is);
buffer = ex.getText();
ex.close();
} else {
System.out.println("请选择后缀为.doc的文件");
}

} catch (Exception e) {
e.printStackTrace();
}
return buffer;
}
public static void main(String[] args) {
Test tp = new Test();
String content = tp.readWord("D:\Program Files\Java\number.doc"); //文件存放的地址
System.out.println("content===="+content);
}
}
编译没问题,运行出现下列问题
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at Test.readWord(Test.java:14)
at Test.main(Test.java:28)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!