Javaでのプロジェクトのsrcフォルダを取得するためのパス

我々は、ファイルのアップロード操作は避けられ行うときのパスはJavaでファイルにアクセスします。

ウェブ上で実行する
1:this.getClass()のgetClassLoader()のgetResource( "/")ある、getPath(); ...
this.getClass()のgetClassLoader()のgetResource( "")ある、getPath();得られたクラスパスです...絶対URIパス。
例えば:/ D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
はSystem.getProperty( "はuser.dir");
this.getClass()のgetClassLoader()。 .getResource( "")ある、getPathは( );. 得られたプロジェクトである絶対パス
例えば:/ D:/jboss-4.2.2.GA/server/default/deploy/hp.war

2:.. This.getClass()のgetResource( "/")ある、getPath();
This.getClass()のgetResource(。 「」).getPath();取得現在のクラスファイルのURIディレクトリです。
例えば:/ D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/
。this.getClass()のgetResourceある、getPath( "")。 ();

3:...にThread.currentThread()getContextClassLoader()のgetResource( "/")ある、getPath()
...にThread.currentThread()getContextClassLoader()のgetResource( "")ある、getPath()クラスパスGET絶対URIパスです。
例えば:/ D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
にThread.currentThread()getContextClassLoader()のgetResourceある、getPath()..( "")。得られたプロジェクトである絶対パス
例えば:/ D:/jboss-4.2.2.GA/server/default/deploy/hp.war

ローカル操作
1:this.getClass()のgetClassLoader()のgetResource( "")ある、getPath(); ...。
この。.getClass()のgetClassLoader()のgetResource ( "")ある、getPath(); .. クラスパスGET絶対URIパスです。
/ MyProjectsで/ HP /ウェブルート/ WEB-INF /クラス:/ D:のような
Xは-ことができる( "")..実行しない; this.getClass()のgetClassLoader()のgetResourceある、getPath()。
2:this.getClass()。
... this.getClass()のgetResource( "")ある、getPath(); を得るには、現在のクラスファイルのURIディレクトリです。
例えば:/ D:/ MyProjectsで/ HP /ウェブルート/ WEB-INF /クラス/ COM /ジェベル/ヘルパー/
/ D:/ MyProjectsで/ HP / WEBROOT /得られたWEB-INF /クラス/クラスパスが絶対URIパスです。
以下のような:/ D:/ MyProjectsで/ HP / WEBROOT / WEB-INF /クラス

3:...にThread.currentThread()getContextClassLoader()のgetResource( "")GETPATH()
..にThread.currentThread()getContextClassLoader()のgetResource ( "").getPath()クラスパスGET絶対URIパスです。
以下のような:/ D:/ MyProjectsで/ HP / WEBROOT / WEB-INF /クラス
...にThread.currentThread()getContextClassLoader()のgetResource( "/")ある、getPath()X-の実行することはできません

最終的に
では、Webアプリケーション、我々は一般的に渡しますServletContext.getRealPath(「/」)を取得する方法、Webアプリケーションルートディレクトリがある絶対パス
そこrequest.getContextPath();使用中のWeblogic request.getServletContext()getContextPath();.が、WebLogicサーバーにデプロイ戦争に包装し、このようにして得られた社内プロジェクトのファイル構造の概念は常にnullでない場合パス未満を取得し、それが具体的な解決策を見つけるためには至っていません

おすすめ

転載: www.cnblogs.com/yyhhblog/p/11280988.html