POI导出Excel报错No such file or directory的解决方法
程序员文章站
2024-04-01 17:38:04
场景重现
apache poi
linux
tomcat
如上所示,当时在linux+tomcat的环境下,使用apache的poi导出excel的时候就会报“...
场景重现
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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。