简体   繁体   English

检索JSF Managed Bean中的Web应用程序根路径

[英]Retrieve the web app root path in JSF Managed 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 我试图访问jsf托管bean中的示例/ web文件夹(见图中的下图),但似乎无法找到一种方法来实现它

我试图访问jsf托管bean中的** example / web **文件夹(见图中的下图),但似乎找不到办法

thx 谢谢

Try 尝试

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

for build relative url's to resources in your app. 用于构建相对URL到应用程序中的资源。

If you want the real path... 如果你想要真正的道路......

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

If you want to get it as a File for some reason, then you need ExternalContext#getRealPath() . 如果由于某种原因想要将其作为File获取,则需要ExternalContext#getRealPath() This converts a relative web path to an absolute disk file system. 这会将相对Web路径转换为绝对磁盘文件系统。 Since you need the web's root folder, just pass in / : 由于您需要web的根文件夹,只需传入/

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

Unrelated to the concrete problem, whatever functional requirement you've had in mind for which you thought that having an absolute local disk file system path to the web folder is the right solution, it has most definitely to be solved differently. 具体问题无关 ,无论你有什么功能要求,你认为拥有一个绝对的本地磁盘文件系统路径到web文件夹是正确的解决方案,它绝对要以不同的方式解决。 And indeed, as per your comment on the other answer, 事实上,根据你对其他答案的评论,

because Im trying to upload some file inside the folder and using the relative path 因为我试图在文件夹中上传一些文件并使用相对路径

you're going the wrong path. 你走错了路。 You should not store uploaded files in there if you intend to keep them longer than the webapp's deployment lifetime. 如果您打算将上传的文件保留的时间长于webapp的部署生命周期,则不应将其存储在那里。 Whenever you redeploy the webapp (and on some server configs even when you restart the server), the uploaded files would get completely lost, simply because they are not contained as part of the original WAR file. 无论何时重新部署webapp(甚至在重新启动服务器时在某些服务器配置上),上传的文件都会完全丢失,因为它们不包含在原始WAR文件中。 Even more, some heavy server configs don't expand the WAR on disk at all, but in memory instead, the getRealPath() would then always return null . 更重要的是,一些繁重的服务器配置根本不会在磁盘上扩展WAR,但在内存中, getRealPath()将始终返回null

Rather store it in a fixed disk file system path outside the server's deploy folder. 而是将其存储在服务器部署文件夹之外的固定磁盘文件系统路径中。 Add that path in turn as a new server context or docroot, so that it's accessible on a different (virtual) context path. 依次将该路径添加为新的服务器上下文或docroot,以便可以在不同的(虚拟)上下文路径上访问它。 Or homegrow a servlet which gets an InputStream of it from disk and writes it to OutputStream of the response. 或者homegrow一个servlet,它从磁盘获取它的InputStream并将其写入响应的OutputStream See also this related answer: Uploaded image only available after refreshing the page 另请参阅此相关答案:上载的图像仅在刷新页面后可用

Try: 尝试:

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

to get real path. 得到真正的道路。

Create a tmp file in resources/temp/ to avoid any exception. 在resources / temp /中创建一个tmp文件以避免任何异常。

Just wanted to thank Balus C. Code Java with JSP, in Tomcat/Tomee server I the following code that works: 只是想感谢Balus C. Code Java with 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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM