通过 apache POI 上传 xlsx 文件时如何修复 AbstractMethodError

How to fix AbstractMethodError when uploading xlsx file through apache POI

我一直在尝试使上传(在旧的网络应用程序中)兼容 xls 和 xlsx 文件,xls 运行 没问题,但上传 xlsx 会引发此错误:

SEVERE: Servlet.service() for servlet [MainServlet] in context with path [/timesheet] threw exception [Servlet execution threw an exception] with root cause
java.lang.AbstractMethodError: org.apache.crimson.tree.ElementNode2.getTextContent()Ljava/lang/String;
    at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.readElement(PackagePropertiesUnmarshaller.java:146)
    at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.loadCreated(PackagePropertiesUnmarshaller.java:162)
    at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.unmarshall(PackagePropertiesUnmarshaller.java:124)
    at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:788)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:327)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:185)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:144)

当前poi lib版本是3.14,但我也试过3.16(同样的错误)和3.17,应用目前使用的是Java 1.6

public List<AttendanceRecord> getAttendanceRecords(List<AttendanceRecordErrMsg> errorMessages) throws Exception {

        Workbook wb = WorkbookFactory.create(new FileInputStream(arFile)); //app already throws an error upon reaching this line

        List<AttendanceRecord> attendanceRecordList = new ArrayList<AttendanceRecord>();

出现这种错误的原因是因为Crimson Library is included in your project, which only provide implementation (base on xml-apis 1.0.b2 as at latest version 1.1.3) on Node interface, and does not implement getTextContent() method that is introduced according to Document Object Model (DOM) Level 3 Core Specification.

要修复此类错误,您可以执行以下操作之一:

1.Remove Crimson Library 来自您的项目。

2.Specify javax.xml.parsers.DocumentBuilderFactory 系统 属性 通过添加以下行
来避免使用 org.apache.crimson.jaxp.DocumentBuilderFactoryImpl class System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");