A method summary for getting the path is implemented in Java

  • 2020-04-01 03:48:02
  • OfStack

Get the path in the Java file

Thread.currentthread ().getcontextclassloader ().getresource ("") // get the path of the resource file (.class file)
This getSystemResource (" ")
Class_Name. Class. GetClassLoader (). GetResource (" ")
Class_Name. Class. GetResource ("/")                  
Class_Name. Class. GetResource (" ")                 // gets the path of the current class
System. GetProperty (" user. Dir ")                           // get the absolute path to the project root directory
System. GetProperty (" Java.class. Path ")       // get the classpath and package path

The printouts are as follows:

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/imp / / com/XML
F: work_litaouri_test
F: work_litaouri_testWebContentWEB - INFclasses; F: work_litaouri_testWebContentWEB - INFlibdom4j. Jar

Two, JSP to get the current application of the relative path and absolute path

The absolute path to the root directory: requeste.getrequesturi ()
Absolute path to the file: application.getrealpath (request-getrequesturi ());
The absolute path of the current web application: application.getrealpath ("/");
New File(application.getrealpath (request-getrequesturi ())).getparent ()

The relative and absolute paths of the current application are obtained in the Servlet

The absolute path to the root directory: requeste.getservletpath ();
Absolute path to file: requeste.getsession ().getservletcontext ().getrealpath (requeste.getrequesturi ())
The absolute path of the current web application: servletConfig getServletContext () getRealPath ("/");

(ServletContext object gets several ways:
Javax.mail. Servlet. HTTP. HttpSession. GetServletContext ()
Javax.mail. Servlet. JSP. PageContext. GetServletContext ()
Javax.mail. Servlet. ServletConfig. GetServletContext ()
)

The above is all the content of this article, I hope you can enjoy it.

Please take a moment to share the article with your friends or leave a comment. We sincerely appreciate your support!


Related articles: