1。Javaファイルのパスを取得します
thread.currentThread()。getContextClassLoader()。getResource( "")//リソースファイル(.classファイル)が配置されているパスを取得します
classloader.getSystemResource( "")
class_name.class.getClassLoader()。getResource( "")
class_name.class .getResource( "/")
class_name.class .getResource( "")//現在のクラスが配置されているパスを取得します
System.getProperty( "user.dir")//プロジェクトのルートディレクトリへの絶対パスを取得します
System.GetProperty( "Java.class.path")//クラスパスとパッケージパスを取得します
印刷物は次のとおりです。
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/com/xml/inp/
F:work_litaouri_test
F:work_litaouri_testwebcontentweb-infclasses;
2。JSPの現在のアプリケーションの相対的および絶対パスを取得します
ルートディレクトリに対応する絶対パス:request.getRequesturi()
ファイルへの絶対パス:application.getRealPath(request.getRequesturi());
現在のWebアプリケーションの絶対パス:application.getRealPath( "/");
要求されたファイルの上部ディレクトリを取得:new file(application.getRealPath(request.getRequesturi()))。
3.サーブレット内の現在のアプリケーションの相対的および絶対パスを取得します
ルートディレクトリに対応する絶対パス:request.getServletPath();
ファイルへの絶対パス:request.getSession()。getServletContext()。getRealPath(request.getRequesturi())
現在のWebアプリケーションの絶対パス:servletconfig.getServletContext()。getRealPath( "/");
(ServletContextオブジェクトはいくつかの方法で取得されます。
javax.servlet.http.httpsession.getServletContext()
javax.servlet.jsp.pagecontext.getServletContext()
javax.servlet.servletconfig.getServletContext()
))
上記はこの記事の全体的な内容です。あなたがそれを気に入っていただければ幸いです。
記事を友達と共有するか、コメントを残してください。ご支援ありがとうございます!