POI export Excel error No such file or directory solution

  • 2020-11-20 06:06:30
  • OfStack

The scene again

Apache POI
Linux
Tomcat

As shown above, in the linux+tomcat environment, the error "No such file or directory" will be reported when exporting excel using poi of apache.

The error message


java.lang.RuntimeException: java.io.IOException: No such file or directory
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:569)
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:558)
    at com.app.util.ExcelIOUtil.write(ExcelIOUtil.java:46)
    at com.app.controllers.DrivingSchoolController.download(DrivingSchoolController.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.mvc.invoke.ActionInvoker.invoke(ActionInvoker.java:75)
    at com.mvc.MvcDispatcher.service(MvcDispatcher.java:119)
    at com.mvc.MvcFilter.doFilter(MvcFilter.java:67)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1810)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662) 

The solution

Let's talk about the solution first, because the solution is very simple, just create a "temp" folder in the root directory of tomcat.

The reason for the error

This error occurs because by default poi exports the exported excel to the system's temporary directory, but the temp folder does not exist under the linux's tomcat at the time, so the directory cannot be found. This is true regardless of the system environment in which the tomcat root directory does not have temp. Take a look at the poi source code below and you will understand.


  public void write(OutputStream stream) throws IOException{
   for (SXSSFSheet sheet : _xFromSxHash.values()){
     sheet.flushRows();
     }
    // Save to a temporary directory 
    File tmplFile = File.createTempFile("poi-sxssf-template", ".xlsx");
    tmplFile.deleteOnExit();
    FileOutputStream os = new FileOutputStream(tmplFile);
    _wb.write(os);
    os.close();

    //Substitute the template entries with the generated sheet data files
    injectData(tmplFile, stream);
    tmplFile.delete();
  }


Related articles: