POI导出Excel报错No such file or directory的解决方法
场景重现
Apache POI
Linux
Tomcat
如上所示,当时在linux+tomcat的环境下,使用apache的poi导出excel的时候就会报“No such file or directory”的错误。
错误信息
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)
解决方案
咱们先说解决方案,因为解决的办法非常简单,只需要在tomcat的根目录下建一个“temp”的文件夹即可。
错误原因
之所以会出现这个错误,是因为poi默认将导出的excel导出到系统的临时目录,而当时那个linux的tomcat下不存在那个temp文件夹,所以就找不到目录了。其实不管是在什么系统环境下,只要tomcat根目录没有temp都会出现这个情况。看看下面poi的源码你就明白了。
public void write(OutputStream stream) throws IOException{ for (SXSSFSheet sheet : _xFromSxHash.values()){ sheet.flushRows(); } //保存到临时目录 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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)