我正在使用Apache.poi打开存储在服务器上的.xlsx文件。
我的测试人员中只有一个在运行应用程序时遇到InvalidFormatException。如果我更改代码以在本地打开.xlsx文件,他没有问题。 他的总部设在美国(我在爱尔兰和服务器一样)。
我得出结论,打开文件的延迟是问题,而poi库正在决定.xlsx文件是否已损坏。
从下面的代码的主要部分切出。 也是从命令行运行的异常日志。 使用java -jar TWRPowerCalculator.jar
我现在必须从服务器中删除.xlsx文件。
package twrpowercalculator;
//Apache Poi maintain a library for manipulating excel file formats
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
//Apache Poi maintain a library for manipulating excel file formats
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellReference;
/**
*
* This class is used for all interaction with the excel file.
*/
public class ExcelInterface {
//Variables
private Sheet sheet;
private Row row;
private Cell cell;
private CellValue cellValue;
private double value;
private Workbook wb = null;
private final String fileName = "TWR_power_calculator.xlsx";
InputStream is;
private FormulaEvaluator evaluator;
public ExcelInterface() {
//Constructor opens excel workbook and formula evaluator for Excel
try {
// Web link needed for link to file on remote web site.
is = new URL("http://www.centronsolutions.com/TWRCalculator/TWR_power_calculator.xlsx").openStream();
wb = WorkbookFactory.create(is); // Needed for link to file on remote web site.
evaluator = wb.getCreationHelper().createFormulaEvaluator(); // Needed for link to file on remote web site. - And needed for direct file load.
}
catch (FileNotFoundException e)
{
System.out.println("The file " + fileName + " was not found.");
}
catch (IOException | InvalidFormatException exception)
{
System.out.println(exception);
}
}
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Lau
ncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImp
l.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherIm
pl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(
LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/openxml4j
/exceptions/InvalidFormatException
at twrpowercalculator.FXMLDocumentController.<init>(FXMLDocumentControll
er.java:36)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:
927)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FX
MLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:22
0)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.ja
va:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at twrpowercalculator.TWRPowerCalculator.start(TWRPowerCalculator.java:3
0)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Platfor
mImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.
java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformI
mpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.ja
va:191)
... 1 more
Caused by: java.lang.ClassNotFoundException:
org.apache.poi.openxml4j.exceptions
.InvalidFormatException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 25 more
Exception running application twrpowercalculator.TWRPowerCalculator