Scene reappears
Apache POI
Linux
Tomcat
As shown above, in the linux+tomcat environment, an error of "No such file or directory" will be reported when using apache poi to export excel.
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)
Solution
Let’s talk about the solution first, because the solution is very simple, you only need to create a “temp” folder in the root directory of tomcat.
Cause of error
The reason for this error is that poi exports the exported excel to the system's temporary directory by default. At that time, the temp folder did not exist under the tomcat of Linux, so the directory cannot be found. In fact, no matter what system environment it is in, as long as the tomcat root directory does not have temp. Just look at the source code of the poi below and you will understand.
public void write(OutputStream stream) throws IOException{ for (SXSSFSheet sheet : _xFromSxHash.values()){ sheet.flushRows(); } //Save to 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(); } The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.