2012-04-30 108 views
9

我試着去訪問一個JSF託管bean的例子/網絡文件夾(參見下圖中)在JSF的web應用程序的根路徑,但不能似乎找到一個方法來做到這一點檢索托管Bean

Im trying to access the **example/web** folder (see below in the image) in a jsf managed bean but cant seem to find a way to do it

THX

回答

13

如果你想獲得它作爲一個File出於某種原因,那麼你需要ExternalContext#getRealPath()。這將相對網絡路徑轉換爲絕對磁盤文件系統。既然你需要Web的根文件夾,只是通過在/

String absoluteWebPath = externalContext.getRealPath("/"); 
File webRoot = new File(absoluteWebPath); 
// ... 

無關到具體的問題,無論功能要求你已經在心中,而您認爲有絕對的本地磁盤文件系統路徑到Web文件夾是正確的解決方案,它有絕對要解決的不同。事實上,按照對對方的回答您的評論,

因爲我試着上傳的文件夾內的一些文件,並使用相對路徑

你走錯了路。如果您打算將上傳的文件保留在Web應用程序的部署生命週期以上,則不應將上傳的文件存儲在那裏。無論何時重新部署Web應用程序(並且即使在重新啓動服務器時也會對某些服務器進行配置),上傳的文件將完全丟失,僅僅是因爲它們不包含在原始WAR文件中。更重要的是,一些沉重的服務器配置根本不會擴展磁盤上的WAR,但是在內存中,getRealPath()將始終返回null

相反,它存儲在服務器的部署文件夾之外的固定磁盤文件系統路徑。依次將該路徑添加爲新的服務器上下文或docroot,以便可以在不同的(虛擬)上下文路徑上訪問該路徑。或者自行創建一個servlet,它從磁盤獲取它的InputStream並將其寫入響應的OutputStream。另見本相關的答案:Uploaded image only available after refreshing the page

+0

感謝您的答案,生病搜索了一些關於存儲圖像和回來,如果我有任何問題 – hari

16

嘗試

FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() 

爲構建相對URL的資源在你的應用程序。

如果你想真實路徑......

ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance() 
      .getExternalContext().getContext(); 
String realPath = ctx.getRealPath("/"); 
+0

很好,謝謝,不過是有可能得到的絕對路徑,因爲我試着上傳的文件夾內的一些文件,並使用相對路徑我得到java.io.FileNotFoundException異常:\示例(訪問被拒絕) – hari

+0

我添加了'realPath' –

0

嘗試:

String relativePath="/resources/temp/"; 
String absolutePath= FacesContext.getCurrentInstance.getExternalContext().getRealPath(relativePath); 
File file = new File(absolutePath); 

得到真實路徑。

在資源/ temp /中創建一個tmp文件以避免任何異常。

0

只想感謝Balus C.JAVA代碼與JSP,到Tomcat/Tomee服務器我下面的代碼工作:

private Boolean SaveUserItemImage(Part ui, String bid) throws IOException { 

    Boolean fileCreate = false; 
    OutputStream out = null; 
    InputStream filecontent = null; 
    ExternalContext ctx = context().getExternalContext(); 
    String absoluteWebPath = ctx.getRealPath("/"); 
    String resource_path = absoluteWebPath + "\\resources\\"; 
    String image_path = resource_path + "\\" + this.itemType + "_images\\"; 
    String buildFileName = image_path + bid + "_" + getFileName(ui); 
    File files = null; 

    try { 
     files = new File(buildFileName); 
     fileCreate = true; 
    } catch (Exception ex) { 
     System.out.println("Error in Creating New File"); 
     Logger.getLogger(ItemBean.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    if (fileCreate == true) { 
     if (files.exists()) { 
     /// User may be using same image file name but has been editted 
     files.delete(); 
     } 

     try { 
     out = new FileOutputStream(files); 
     filecontent = ui.getInputStream(); 
     int read = 0; 
     final byte[] bytes = new byte[1024]; 
     while ((read = filecontent.read(bytes)) != -1) { 
      out.write(bytes, 0, read); 
     } 
     fileCreate = true; 
     } catch (FileNotFoundException fne) { 
     fileCreate = false; 
     Logger.getLogger(ItemBean.class.getName()).log(Level.SEVERE, "SaveUserItemImage", fne); 
     } finally { 
     if (out != null) { 
      out.close(); 
     } 
     if (filecontent != null) { 
      filecontent.close(); 
     } 
     files = null; 
     } 
    } 
    return fileCreate; 
    }