Retrieve the web app root path in JSF Managed Bean
Asked Answered
P

4

9

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

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

Piane answered 30/4, 2012 at 1:47 Comment(0)
E
14

If you want to get it as a File for some reason, then you need ExternalContext#getRealPath(). This converts a relative web path to an absolute disk file system. Since you need the web's root folder, just pass in /:

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. 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. 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. 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.

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. Or homegrow a servlet which gets an InputStream of it from disk and writes it to OutputStream of the response. See also this related answer: Uploaded image only available after refreshing the page

Ernaernald answered 30/4, 2012 at 17:59 Comment(1)
thanks for your answer,Ill search a bit more on storing images and get back if I have any questionPiane
I
17

Try

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

for build relative url's to resources in your app.

If you want the real path...

ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
            .getExternalContext().getContext();
String realPath = ctx.getRealPath("/");
Interoffice answered 30/4, 2012 at 1:58 Comment(1)
nice, thanks but is it possible to get the absolute path, because Im trying to upload some file inside the folder and using the relative path Im getting java.io.FileNotFoundException: \example (Access is denied)Piane
E
14

If you want to get it as a File for some reason, then you need ExternalContext#getRealPath(). This converts a relative web path to an absolute disk file system. Since you need the web's root folder, just pass in /:

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. 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. 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. 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.

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. Or homegrow a servlet which gets an InputStream of it from disk and writes it to OutputStream of the response. See also this related answer: Uploaded image only available after refreshing the page

Ernaernald answered 30/4, 2012 at 17:59 Comment(1)
thanks for your answer,Ill search a bit more on storing images and get back if I have any questionPiane
W
0

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.

Wandering answered 29/1, 2016 at 10:34 Comment(0)
Z
0

Just wanted to thank Balus C. Code Java with JSP, in Tomcat/Tomee server I the following code that works:

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;
  }
Ziegfeld answered 17/2, 2017 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.