Getting the path to the file in Java is unavoidable when we do the upload file operation.
Running on the web
1:
This.getclass (). getClassLoader (). GetResource ("/"). GetPath ();
This.getclass (). getClassLoader (). GetResource (""). GetPath (); The absolute URI path of the classpath is obtained.
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war/web-inf/classes/
System.getproperty ("User.dir");
This.getclass (). getClassLoader (). GetResource ("."). GetPath (); The absolute path to the project is obtained.
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war
2:
This.getclass (). GetResource ("/"). GetPath ();
This.getclass (). GetResource (""). GetPath (); Gets the URI directory of the current class file. Don't include yourself!
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war/web-inf/classes/com/jebel/helper/
This.getclass (). GetResource ("."). GetPath (); X cannot be run
3:
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath ()
Thread.CurrentThread (). Getcontextclassloader (). GetResource (""). GetPath () Gets the absolute URI path of the classpath.
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war/web-inf/classes/
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("."). GetPath () Gets the absolute path of the project.
such as:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war
In the local run
1:
This.getclass (). getClassLoader (). GetResource (""). GetPath ();
This.getclass (). getClassLoader (). GetResource ("."). GetPath (); The absolute URI path of the classpath is obtained.
such as:/d:/myprojects/hp/webroot/web-inf/classes
This.getclass (). getClassLoader (). GetResource ("."). GetPath (); X cannot be run
2:
This.getclass (). GetResource (""). GetPath ();
This.getclass (). GetResource ("."). GetPath (); Gets the URI directory of the current class file. Don't include yourself!
such as:/d:/myprojects/hp/webroot/web-inf/classes/com/jebel/helper/
/d:/myprojects/hp/webroot/web-inf/classes/gets the absolute URI path of the classpath.
such as:/d:/myprojects/hp/webroot/web-inf/classes
3:
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("." ). GetPath () Thread.CurrentThread (). Getcontextclassloader (). GetResource(""). GetPath () got the The absolute URI path of the classpath: such as:/d:/myprojects/hp/webroot/web-inf/classesthread.currentthread (). Getcontextclassloader (). GetResource ("/"). GetPath () X cannot be run
At last
In a Web application, we generally get the absolute path to the root of the Web application through the Servletcontext.getrealpath ("/") method.
There are also Request.getcontextpath (); In WebLogic, use Request.getservletcontext (). Getcontextpath (); But if packaged into a war to deploy to a WebLogic server, there is no concept of a file structure inside the project, and in this way always get null , the path is not available and no specific solution has been found yet.
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/xiuhaijuanqiang/archive/2011/01/14/6137949.aspx
Java upload file to get directory with the way