Http://www.cn-java.com/www1? Action-viewnews-itemid-78354
The paths used in Java are divided into absolute paths and relative paths. In essence, Java can only use absolute paths to find resources. All relative paths are just some convenient methods to find resources. However, the API helps us build an absolute path at the underlying level to find resources!
When developing Web applications, you often need to obtain the physical path of the current webroot on the server.
For servlet, action, controller, or filter, listener, interceptor and other related classes, we only need to obtain servletcontext and then use servletcontext. getrealpath ("/") to obtain the physical address of the current application on the server.
If servletcontext is not obtained in the class, there are two ways to do this:
1. use the class loading mechanism of Java to call XXX. class. getclassloader (). getresource (""); method to get the classpath, and then process to get the webroot directory, this method can only be effective under the webroot/WEB-INF/classes, if the class is packaged into a jar file, the method is invalid. In this case, we should use the following method.
2. the idea of Spring framework in WEB-INF/web. in XML, create a webapprootkey PARAM and specify a value (default: webapp. root) as the key value, and then use listener, filter, or servlet to execute string webapprootkey = getservletcontext (). getrealpath. the root serves as the key and the value is written to the system properties. Then, use system. getproperty ("webapp. Root") in the program to obtain the physical path of webroot.
Based on the second idea, we can expand it. However, for applications deployed on a server, if you do not need them yet, please read them further.
The following are some methods to obtain the absolute path of classpath and the current class. You can use some of these methods to obtain the absolute path of the required resources:
1. debitnoteaction. Class. getresource ("")
The URI Directory of the current filetest. Class file is obtained. Not yourself!
For example: file:/D:/Eclipse/springtest/webroot/WEB-INF/classes/
Atacarnet/src/COM/EVI/modules/atacarnet/Action/
2. debitnoteaction. Class. getresource ("/")
Obtain the absolute URI path of the current classpath.
For example: file:/D:/Eclipse/springtest/webroot/WEB-INF/classes/
3. thread. currentthread (). getcontextclassloader (). getresource ("")
The obtained absolute URI path of the current classpath
For example: file:/D:/Eclipse/springtest/webroot/WEB-INF/classes/
4. debitnoteaction. Class. getclassloader (). getresource ("") or classloader. getsystemresource ("")
The obtained absolute URI path of the current classpath.
For example: file:/D:/Eclipse/springtest/webroot/WEB-INF/classes/
5. Obtain the relative server path
System. getproperty ("user. dir ")
Example: e:/Apache-Tomcat-5.5.16/Apache-Tomcat-5.5.16/bin
We recommend that you use thread. currentthread (). getcontextclassloader (). getresource ("") to obtain the URI representation of the absolute path of the current classpath.
6. Obtain the absolute path in the project
Generally, request. getrealpath ("/") or request. getrealpath ("/config /")
However, we do not recommend using request. getrealpath ("/"). You can use the servletcontext. getrealpath ("/") method to obtain the absolute path of the root directory of the Web application.
It is very easy to obtain SRC files, because SRC is the default relative directory. For example, if you want to obtain the test. Java file in the com directory under SRC, you only need to do this.
File F = new file (COM/test. Java );
But if I want to retrieve files not in the src directory or webroot directory, I want to retrieve them from directories at the same level as SRC or webroot, for example, Doc.
My hard method is implemented as follows:
String Path = This. getservletcontext (). getrealpath ("/");
Properties P = new properties ();
P. load (New fileinputstream (new file (path. substring (0, (path. lastindexof ("// webroot") + 1) + "DOC/DB. properties ")));
System. Out. println (P. getproperty ("drivername "));