2014-07-11 61 views
1

的java.io.InputStream這類似的問題:的Struts 2 - HTTP狀態500 - 無法找到名爲

Question A

Question B

但我不能適用於解決方案我的情況。也許有人可以在我的代碼中發現錯誤。

我想生成Excel文件在我的Struts2的應用程序,讓用戶下載,但我得到一個錯誤:

「HTTP狀態500 - 無法找到與該名字的java.io.InputStream」

使用調試器我已經確認,正確的輸入流與文件內容創建和方法返回'成功',所以似乎Struts沒有拿起這個流。

struts.xml我:

<action name="exportReferences" class="manageRefAction" method="exportReferences"> 
     <result name="success" type="stream"> 
      <param name="contentType">application/vnd.ms-excel;charset=utf-8</param> 
      <param name="contentDisposition">contentDisposition</param> 
      <param name="inputName">excelStream</param> 
      <param name="bufferSize">1024</param> 
     </result> 
      (...) 
    </action> 

然後在ManageRefAction I類有:

private InputStream excelStream; 

    public InputStream getExcelStream() { 
     return this.excelStream; 
    } 

public String exportReferences() { 

     List<ReferenceData> referenceXLSList = null; 
     String exportFilename = null; 
     String filename = ""; 
     String retrunStr = process(); 

     Map session = (Map) ActionContext.getContext().get("session"); 

     if (retrunStr.equalsIgnoreCase(ActionSupport.LOGIN)) { 
      setLog("2"); 

      return LOGIN; 
     } else { 
      //Export only data with user's locale 
      ArrayList userLocale = (ArrayList) session.get("userLocale"); 
      List<ArrayList> list = Arrays.asList(userLocale); 
      try { 
       referenceXLSList = referenceDataService.fetchReferenceDataXls(); 
       // Data filtering 
       for (Iterator<ReferenceData> iterator = referenceXLSList.iterator(); iterator.hasNext();) { 
        ReferenceData ref = iterator.next(); 
        String l = ref.getLocal().toLowerCase(); 
        if (!userLocale.contains(l)) { 
         iterator.remove(); 
        } 
       } 
       if (referenceXLSList.isEmpty()) { 
        return INPUT; 
       } 
       ServletContext servletContext = ServletActionContext.getServletContext(); 
       String exportFolderPath = servletContext.getRealPath("/files" + "/xls"); 

       File folder = new File(exportFolderPath); 
       // Creation of the folder if it is not existing 
       if (!folder.exists()) 
        folder.mkdirs(); 

       exportFilename = PLConstants.REF_SHEET_NAME + PLConstants.XLS_FILE_EXTENSION; 
       filename = exportFolderPath + "/" + exportFilename; 
       File file = new File(filename); 
       file.createNewFile(); 
       ExcelWriter writer = new ExcelWriter(); 
       Workbook wb = writer.writeRefData(filename, exportFilename, referenceXLSList); 
       setContentDisposition("attachment; filename=" + exportFilename); 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       wb.write(baos); 
       this.excelStream = new ByteArrayInputStream(baos.toByteArray()); 
       return SUCCESS; 
      } catch (Throwable e) { 
       e.printStackTrace(); 
       logger.info("Exception while exporting partner references", e); 
       return INPUT; 
      } 
     } 

    } 
+0

你有'contentDisposition'吸氣的exception.use? –

+0

我沒有getter,但我設置了setContentDisposition(「attachment; filename =」+ exportFilename); – Michlis

+0

它應該可用於結果,所以你必須創建一個getter。 –

回答

1

試試這個,contentDisposition是struts.xml中沒有得到的字符串值。 所以,它拋出這句法${contentDisposition}在參數

<action name="exportReferences" class="manageRefAction" method="exportReferences"> 
    <result name="success" type="stream"> 
     <param name="contentType">application/vnd.ms-excel;charset=utf-8</param> 
     <param name="contentDisposition">${contentDisposition}</param> 
     <param name="inputName">excelStream</param> 
     <param name="bufferSize">1024</param> 
    </result> 
     (...) 
    </action> 
+0

沒有幫助... – Michlis

相關問題