Skip to content

Instantly share code, notes, and snippets.

@datacorner
Created May 17, 2018 12:30
Show Gist options
  • Save datacorner/e9fd9cd1ee1cb34b25afd33bc174b574 to your computer and use it in GitHub Desktop.
Save datacorner/e9fd9cd1ee1cb34b25afd33bc174b574 to your computer and use it in GitHub Desktop.
Returns the real system path of WEB-INF
/**
* Returns the real system path of WEB-INF through a servlet
* @return répertoire WEB-INF
*/
public static String WEBINF_PATH() {
String className = JoyConfigfileProvider.class.getName().replaceAll("\\.", "/") + ".class";
// Use the ClassLoader to find the absolute path to this file.
URL classPath = JoyConfigfileProvider.class.getClassLoader().getResource(className);
//create a new File and go from file to parent file to find the WEB-INF directory
File f = new File(classPath.getPath());
while (f != null && !f.getName().equals("WEB-INF"))
f = f.getParentFile();
//if the root is reached without finding the WEB-INF directory WEB-INF will equal null
if (f != null)
return f.getPath();
else
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment