Method of getting current directory in jsp

  • 2021-07-01 08:04:19
  • OfStack

This article describes the example of jsp to obtain the current directory of the implementation method, to share for your reference. The specific implementation method is as follows:

1. Use the System. getProperty () function to get the current path:

System.out.println(System.getProperty("user.dir"));//user.dir The current path is specified 

2. Use the function provided by File to get the current path:
File directory = new File("");// Set as current folder 
try{
System.out.println(directory.getCanonicalPath());// Get the standard path
System.out.println(directory.getAbsolutePath());// Get Absolute Path
}catch(Exceptin e){}

File. getCanonicalPath () and File. getAbsolutePath () differ only about the new File (".") and new File (".") paths.

# For the getCanonicalPath () function, "." denotes the current folder, and ".." denotes the next level of the current folder
# For the getAbsolutePath () function, return the current path plus the path you set when new File (), regardless of ".", ".."
# As for the getPath () function, all you get is the path you set when new File ()

For example, the current path is C: test:

File directory = new File("abc");
directory.getCanonicalPath(); // What I get is C:testabc
directory.getAbsolutePath(); // What I get is C:testabc
direcotry.getPath(); // What I get is abc
File directory = new File(".");
directory.getCanonicalPath(); // What I get is C:test
directory.getAbsolutePath(); // What I get is C:test.
direcotry.getPath(); // What I get is .
File directory = new File("..");
directory.getCanonicalPath(); // What I get is C:
directory.getAbsolutePath(); // What I get is C:test..
direcotry.getPath(); // What I get is ..

Get the current working directory of the JAVA program

File file = new File("t.tmp");
String fullpath = file.getAbsolutePath();

① request. getRealPath:

Method: request. getRealPath ("/")
The resulting path: C: Program FilesApache Software FoundationTomcat 5.5 webappsstrutsTest

Method: request. getRealPath (".")
The resulting path: C: Program FilesApache Software FoundationTomcat 5.5 webappsstrutsTest.

Method: request. getRealPath ("")
The resulting path: C: Program FilesApache Software FoundationTomcat 5.5 webappsstrutsTest

Method: request. getRealPath ("web. xml")
The resulting path: C: Program FilesApache ES90FoundationTomcat 5.5 webappsstrutsTestweb. xml

② request. getParameter ("");
ActionForm.getMyFile();
Methods: String filepath = request. getParameter ("myFile");
Resulted path: D: VSS Installation Directory users. txt

Methods: String filepath = ActionForm. getMyFile ();
Resulted path: D: VSS Installation Directory users. txt

I hope this article is helpful to everyone's jsp programming.


Related articles: