2017-05-03 16 views
0

この例外を修正するにはどうすればよいですか? zipに複数のxlsxを追加する必要があります。最初のxlsxは、リストに追加されますが、もう一つは、このエラーがスローされます。Zip Outputstreamが閉じています。 java.io.IOException:ストリームが閉じられた

public void downloadData() throws Exception { 

    FacesContext fc = FacesContext.getCurrentInstance(); 
    setSourceList(new ArrayList<String>()); 
    setTargetList(new ArrayList<String>()); 

    List<String> tempList = dualList.getTarget(); 
    System.out.println(tempList.size()); 

    if (tempList != null && tempList.size() > 0) { 

     ExternalContext ec = fc.getExternalContext(); 
     ec.responseReset(); 

     ec.setResponseContentType("application/zip"); 
     ec.setResponseHeader("Content-Disposition", "attachment; filename=\"Export.zip\""); 
     ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); 
     ZipOutputStream zip = new ZipOutputStream(outByteStream); 
     OutputStream outStream = ec.getResponseOutputStream(); 

     for (int i = 0; i < tempList.size(); i++) { 

      outByteStream = new ByteArrayOutputStream(); 
      //zip = new ZipOutputStream(outByteStream); 
      // outStream = ec.getResponseOutputStream(); 

      int id = ownerNameIdMap.get(tempList.get(i)); 
      String oName = tempList.get(i); 
      String fileName = oName + ".xlsx"; 

      if (petList != null && petList.size() > 0) { 
       petList.clear(); 
      } 

      workBook = new XSSFWorkbook(); 

      OwnerModel ownerModel = ownerMap.get(id); 
      ownerFirstName = ownerModel.getFirstName(); 
      ownerLastName = ownerModel.getLastName(); 
      workSheet = workBook.createSheet(ownerLastName + ", " + ownerFirstName); 
      petList = iOwnerRepository.getActivePetsOfOwner(ownerModel.getId()); 
      petCount = petList.size(); 
      createMasterSheet(); 
      renderSheet(ownerModel); 

      workBook.write(outByteStream); 
      //addEntry(zip, fileName, outByteStream); 
      ZipEntry entry = new ZipEntry(fileName); 
      zip.putNextEntry(entry); 
      System.out.println(fileName); 
      zip.write(outByteStream.toByteArray()); 
      zip.closeEntry(); 
      workBook.write(zip); 
      outStream.write(outByteStream.toByteArray()); 


     } 

     outStream.flush(); 
     outStream.close(); 
     zip.close(); 
     fc.responseComplete(); 

    } 

} 

私のスタックトレース

SEVERE: Received 'java.io.IOException' when invoking action listener '#{exportViewBean.downloadData}' for component 'j_idt83' 
May 03, 2017 11:24:13 AM javax.faces.event.MethodExpressionActionListener processAction 
SEVERE: java.io.IOException: Stream closed 
    at java.util.zip.ZipOutputStream.ensureOpen(ZipOutputStream.java:97) 
    at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:190) 
    at com.fetchinglife.modules.dataimport.views.ExportViewBean.downloadData(ExportViewBean.java:218) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297) 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153) 
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:771) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:300) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:105) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
+0

あなたのコードフラグメントのどの行が218行を表していますか? – Thomas

+0

zip.putNextEntry(エントリ);この行 – rParvathi

+0

@トーマス何か解決策がありますか? – rParvathi

答えて

0

outByteStreamの使用を統合してみます。 forループの前に開いて、zipを初期化しますが、forループ内に別のByteArrayOutputStreamインスタンスを割り当ててoutByteStreamに割り当てた後、このループを使用します。私はこれがあなたの例外を引き起こすかどうかはわかりませんが、試してみる価値があるかもしれません...

+0

ありがとうございます。私は試してみる – rParvathi

+0

私は試みたが、仕事をしなかった。アーカイブを開くことができません。 – rParvathi

関連する問題