1. 程式人生 > >菜鳥調錯——POI匯出Excel報錯No such file or directory

菜鳥調錯——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(); }