2010-08-06 80 views
14

這是使用JSF?下載文件的正確方式,只需將文件鏈接?在那種情況下,我如何獲得文件的URL?使用JSF下載文件?

我已經看到了使用的BufferedInputStream一個例子:

有什麼區別?

感謝

回答

23

如果它是一個簡單的文件,只需將公共web內容(那裏有你把你靜態和JSF文件),並創建一個鏈接。

<h:outputLink value="/files/file.ext">link</h:outputLink> 

servletcontainer將擔心應用正確的標題。

如果由於某些特定原因(例如,在服務器計算機或數據庫中的固定路徑中)它位於公共webcontent之外,則創建一個獲取其InputStream的servlet並將其寫入響應的OutputStream沿着至少Content-TypeContent-DispositionContent-Length標題。你可以找到here一個簡單的開球的例子。此外,可以簡單地鏈接到servlet的url-pattern

如果它是動態地生成並根據JSF特定的請求參數,那麼你也可以在由h:commandLinkh:commandButton約束託管bean的動作做,但你只需要確保您在結束通話FacesContext#responseComplete() bean的動作方法,以防止JSF手中的導航。可以重用相同類型的servlet代碼來傳輸文件。你可以在this answer找到一個開球的例子。

+0

吳THX BalusC 我無法連接在所有周末,但我必須感謝你的所有幫助= D 我真的很感激 – ErVeY 2010-08-09 13:34:45

+0

不客氣。 – BalusC 2010-08-09 13:37:08

12

我需要做出類似的代碼,通過JSF

這是在我的JSF頁面

<h:commandButton value="Download" action="#{helloBean.downloadFile}" /> 

我的下載按鈕下載一個文件,它是我的Java代碼

public void downloadFile() { 

    File file = new File("/home/marco/file.txt"); 
    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); 

    response.setHeader("Content-Disposition", "attachment;filename=file.txt"); 
    response.setContentLength((int) file.length()); 
    ServletOutputStream out = null; 
    try { 
     FileInputStream input = new FileInputStream(file); 
     byte[] buffer = new byte[1024]; 
     out = response.getOutputStream(); 
     int i = 0; 
     while ((i = input.read(buffer)) != -1) { 
      out.write(buffer); 
      out.flush(); 
     } 
     FacesContext.getCurrentInstance().getResponseComplete(); 
    } catch (IOException err) { 
     err.printStackTrace(); 
    } finally { 
     try { 
      if (out != null) { 
       out.close(); 
      } 
     } catch (IOException err) { 
      err.printStackTrace(); 
     } 
    } 

} 
+2

命令按鈕的操作方法不應該返回一個String嗎? – codingsplash 2013-08-23 09:34:47

+0

這不是必需的 – 2014-04-01 13:15:52

-1

我有發生錯誤

FacesContext.getCurrentInstance().getResponseComplete(); 

從類型

java.lang.IllegalStateException: getOutputStream() has already been called for this response 

,我解決了這個問題:

JSF頁面:

<h:commandButton action="#{bean.downloadFile}" id="downloadBtn" value="Download"/> 

Bean方法:

public void downloadFile(File file) { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); 
    response.setHeader("Content-Disposition", "attachment;filename=file.txt"); 
    response.setContentLength((int) file.length()); 
    FileInputStream input= null; 
    try { 
     int i= 0; 
     input = new FileInputStream(file); 
     byte[] buffer = new byte[1024]; 
     while ((i = input.read(buffer)) != -1) { 
      response.getOutputStream().write(buffer); 
      response.getOutputStream().flush(); 
     }    
     facesContext.responseComplete(); 
     facesContext.renderResponse(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if(input != null) { 
       input.close(); 
      } 
     } catch(IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

我說試試因爲我沒有時間解釋它,帖子的標題是[用JSF下載文件?]。我已經測試它,它的工作原理,如果你沒有測試,請不要告訴我想... – 2015-09-07 05:45:04