Tomcat webapps full-text directory?

I have tomcat extraction in one place and my webapps directory could be somewhere else. Then, how do I get the absolute path to my web application ?, I have a file processing program inside webapps, and I want to find the absolute path to my web folders or the directory of my application (starting with c: / or / home / use /. ./../webapp/mywebapplication). The fact is that I do not control who / where my application will be deployed?

+3
source share
2 answers

. getServletContext(). getRealPath ("") - , .war.

. C:/Program Files/Tomcat 6/webapps/myapp/WEB-INF/classes/somefile.properties:

// URL returned "/C:/Program%20Files/Tomcat%206.0/webapps/myapp/WEB-INF/classes/"
URL r = this.getClass().getResource("/");

// path decoded "/C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
String decoded = URLDecoder.decode(r.getFile(), "UTF-8");

if (decoded.startsWith("/")) {
    // path "C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
    decoded = decoded.replaceFirst("/", "");
}
File f = new File(decoded, "somefile.properties");
+2

Source: https://habr.com/ru/post/1714395/


All Articles