2012-04-30 103 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; 
    }