gpt4 book ai didi

java - 尽管将非空输入流传递给 WorkbookFactory 仍获取 EmptyFileException

转载 作者:行者123 更新时间:2023-11-30 05:58:04 29 4
gpt4 key购买 nike

下面的代码返回 EmptyFileException,尽管文件不为空(如输出中所示):

public class RecordImpl {

private AssociateImpl associate;

public AssociateImpl getAssociate() {
return associate;
}

public void setAssociate(AssociateImpl associate) {
this.associate = associate;
}


public Document createInsertRecord()throws IOException, InvalidFormatException {

String msg = "";
InputStream inputStream = associate.getFileInputStream();
byte[] binaryRep = IOUtils.toByteArray(inputStream);
System.out.println("File length: "+binaryRep.length);
String year = getYear(inputStream, associate.getPosition());

Document newRecord = new Document();

/*remaining code*/

return newRecord;
}


public String getYear(InputStream inputStream, String position)throws IOException, InvalidFormatException {

System.out.println("Inside RecordImpl: getYear()");
//code throws error at the line below
Workbook workbook = WorkbookFactory.create(associate.getFileInputStream());
Sheet sheet = workbook.getSheet(position);

DataFormatter df = new DataFormatter();
Iterator<Row> rowIterator = sheet.iterator();

String year = "";

/*remaining code*/

return year;
}

}

字节数组的长度显示为21787,说明输入流不为空。然而空文件错误被抛出。带有堆栈跟踪的输出如下所示:

File length: 21787
Oct 15, 2018 12:11:14 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/my-goals] threw exception [org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)] with root cause
org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:74)
at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:57)
at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:135)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:177)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:149)
at com.cerner.model.impl.RecordImpl.getYear(RecordImpl.java:64)
at com.cerner.model.impl.RecordImpl.createInsertRecord(RecordImpl.java:42)
at com.cerner.model.impl.ActionImpl.insertNewRecord(ActionImpl.java:71)
at com.cerner.model.impl.ActionImpl.performAction(ActionImpl.java:43)
at com.cerner.my_goals.GoalResource.uploadFile(GoalResource.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:143)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:160)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:203)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:303)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

传递的文件肯定是一个非空的 XLSX 文件。关于问题可能是什么的任何想法吗?

最佳答案

我认为你的电话:

byte[] binaryRep = IOUtils.toByteArray(inputStream);

inputStream 将移动到流的末尾,以便剩余字节为 0

关于java - 尽管将非空输入流传递给 WorkbookFactory 仍获取 EmptyFileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52811258/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com