多平台统一管理软件接口,如何实现多平台统一管理软件接口
636
2023-03-14
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.SXSJBmOnQTYeVSFWorkbook.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:118http://)
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”的文件夹即JBmOnQTYeV可。
错误原因
之所以会出现这个错误,是因为poi默认将导出的excel导出到系统的临时目录,而当时那个linux的tomcat下不存在那个temp文JBmOnQTYeV件夹,所以就找不到目录了。其实不管是在什么系统环境下,只要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();
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~