4

我有一个读取 .xlsx 文件并向用户显示内容的应用程序。该应用程序在 Windows 环境下运行良好。

我在 ubuntu 服务器上的 tomcat6 上部署了这个 Web 应用程序的 .war 文件。我还复制了服务器上的 .xlsx 文件。

代码中文件的路径是正确的。

但是线

FileInputStream file = new FileInputStream(new File(FileName));
XSSFWorkbook workbook = new XSSFWorkbook(file);

给出错误

java.lang.reflect.InvocationTargetException
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)
org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:207)
com.qm.action.GetProjectNames.execute(GetProjectNames.java:107)

我已检查变量 FileName 是否包含服务器上文件的正确路径和文件名(/usr/local/Metrics/MetricFiles/FY2013_Q2_GIT_Review_Metrics_by_LSS-GC.xlsx)

由于 ubunut 服务器是一个虚拟机,我使用 WinSCP 复制了 .xlsx 文件。文件的大小也是正确的。

为什么linux平台会出现这个错误?

添加附加异常跟踪

Caused by: java.lang.reflect.InvocationTargetException at 
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at 
java.lang.reflect.Constructor.newInstance(Constructor.java:525) at 
org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60) ... 68 more 
Caused by: java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOfRange(Arrays.java:2694) at java.lang.String.<init>(String.java:203) at 
org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseCdataLiteral(PiccoloLexer.java:3027) at 
org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseQuotedTagValue(PiccoloLexer.java:2936) at 
org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseAttributesNS(PiccoloLexer.java:1754) at 
org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseOpenTagNS(PiccoloLexer.java:1521) at 
org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseTagNS(PiccoloLexer.java:1362) at 
org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.yylex(PiccoloLexer.java:4678) at 
org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yylex(Piccolo.java:1290) at 
org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yyparse(Piccolo.java:1400) at 
org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:714) at 
org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3439) at 
org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1270) at 
org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1257) at 
org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345) at 
org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument$Factory.parse(Unknown Source) at 
org.apache.poi.xssf.model.StylesTable.readFrom(StylesTable.java:121) at 
org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:92) at 
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at 
java.lang.reflect.Constructor.newInstance(Constructor.java:525) at 
org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60) at 
org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403) at 
org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155) at 
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:207) at 
com.qm.action.GetProjectNames.execute(GetProjectNames.java:107) at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at 
java.lang.reflect.Method.invoke(Method.java:601) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
4

4 回答 4

2

将评论推广到答案...

您的堆栈跟踪的关键部分是:

Caused by: java.lang.OutOfMemoryError: Java heap space at 
java.util.Arrays.copyOfRange(Arrays.java:2694)

这告诉我们 Java 没有分配足够的内存来存储处理文件所需的所有数据

如果您在增加 JVM 堆大小方面需要帮助,我建议您查看上一个问题这个问题,这两个问题都可以帮助您解决所有问题。

于 2013-05-31T12:45:12.373 回答
1

读取 .xlsx 文件时我也遇到了同样的异常

org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
Caused By :java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlBeans.typeSystemForClassLoadern

然后我删除了我的旧 xmlbeans-2.0.jar 并放入了包含此方法的最新xmlbeans-2.3.0.jar ,现在它已解决

于 2014-01-29T07:03:47.580 回答
1

我已插入org.apache.xmlbeans项目中。它有帮助。

于 2015-05-12T15:26:52.270 回答
0

像其他答案一样:添加此依赖项:

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.6.0</version>
</dependency>

之后它将起作用。

事实上 poi-ooxml 需要 poi 和 poi-ooxml-schemas 但 poi-ooxml-schemas 需要 xmlbeans

您可以在此页面https://poi.apache.org/overview.html中查看这些先决条件

于 2016-10-25T09:29:55.217 回答