Path to get the project src folder in java

Path to access a file in java when we do file upload operation is inevitable.

Run on the web
. 1: this.getClass () getClassLoader () the getResource ( "/") getPath ();...
this.getClass () getClassLoader () the getResource ( "") getPath (); obtained is ClassPath... the absolute URI path.
Such as: / D: /jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
the System.getProperty ( "user.dir");
this.getClass () getClassLoader (). .getResource ( ".") getPath ( );. the resulting project is an absolute path .
Such as: / D: /jboss-4.2.2.GA/server/default/deploy/hp.war

2:.. This.getClass () the getResource ( "/") getPath ();
. This.getClass () the getResource ( "") .getPath (); get is URI directory of the current class file.
Such as: / D: /jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/
this.getClass () the getResource getPath. ( "."). ();

3:... Thread.currentThread () getContextClassLoader () getResource ( "/") getPath ()
... Thread.currentThread () getContextClassLoader () getResource ( "") getPath () ClassPath get is an absolute URI path.
Such as: / D: /jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
Thread.currentThread () getContextClassLoader () the getResource getPath ().. ( "."). the resulting project is an absolute path .
Such as: / D: /jboss-4.2.2.GA/server/default/deploy/hp.war

local operation
. 1: this.getClass () getClassLoader () the getResource ( "") getPath ();...
The this . .getClass () getClassLoader () getResource ( ".") getPath ();.. ClassPath get is an absolute URI path.
Such as: / D: / myprojects / HP / the WebRoot / the WEB-INF / classes
this.getClass () getClassLoader () the getResource getPath (); X-can not run.. ( ".").
2: this.getClass ().
.. this.getClass () getResource ( " .") getPath (); get is URI directory of the current class file.
Such as: / D: / myprojects / HP / the WebRoot / the WEB-INF / classes / COM / Jebel / Helper /
/ D: / myprojects / HP / the WebRoot / the WEB-INF / classes / ClassPath obtained is an absolute URI path.
Such as: / D: / myprojects / HP / the WebRoot / the WEB-INF / classes

. 3:... Thread.currentThread () getContextClassLoader () the getResource ( ".") GetPath ()
.. Thread.currentThread () getContextClassLoader () the getResource ( "") .getPath () ClassPath get is an absolute URI path. .
Such as: / D: / myprojects / the HP / WebRoot / the WEB-INF / classes
... Thread.currentThread () getContextClassLoader () getResource ( "/") getPath () can not run the X-

final
in a Web application , we generally pass ServletContext.getRealPath ( "/") method to get the Web application 's root directory is the absolute path .
There request.getContextPath (); in the use Weblogic request.getServletContext () getContextPath ();. But if packaged into a war deployed to Weblogic server, and no concept of internal project file structure, obtained in this way is always null obtaining less than the path, it has yet to find concrete solutions

Guess you like

Origin www.cnblogs.com/yyhhblog/p/11280988.html
Recommended