Various simple implementation programs for obtaining the current directory in jsp

Source: Internet
Author: User

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

The code is as follows: Copy code
System. out. println (System. getProperty ("user. dir"); // user. dir specifies the current path.

2. Use the function provided by File to obtain the current path:

The code is as follows: Copy code
File directory = new File (""); // Set it to the current folder
Try {
System. out. println (directory. getCanonicalPath (); // Obtain the standard path.
System. out. println (directory. getAbsolutePath (); // Obtain the absolute path.
} Catch (shorttin e ){}

File. getCanonicalPath () and File. getAbsolutePath () are only different for new File (".") and new File.

# For the getCanonicalPath () function, "." indicates the current folder, and ".." indicates the upper-level folder of the current folder.
# For the getAbsolutePath () function, no matter "." or ".", the current path is returned and the path you set when new File () is added.
# As for the getPath () function, all you get is the path you set in new File ().

For example, the current path is C: test:

The code is as follows: Copy code

File directory = new File ("abc ");
Directory. getCanonicalPath (); // C: testabc
Directory. getAbsolutePath (); // C: testabc
Direcotry. getPath (); // abc is returned.

File directory = new File (".");
Directory. getCanonicalPath (); // C: test
Directory. getAbsolutePath (); // The C: test.
Direcotry. getPath (); // The result is.

File directory = new File ("..");
Directory. getCanonicalPath (); // C:
Directory. getAbsolutePath (); // The C: test ..
Direcotry. getPath (); // The result is ..


Obtains the current working directory of the JAVA program.

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

1. request. getRealPath

Method: request. getRealPath ("/")
Path: C: Program FilesApache Software FoundationTomcat 5.5 webjavasstrutstest

Method: request. getRealPath (".")
Path: C: Program FilesApache Software FoundationTomcat 5.5web+strutstest.

Method: request. getRealPath ("")
Path: C: Program FilesApache Software FoundationTomcat 5.5 webjavasstrutstest

Request. getRealPath ("web. xml ")
C: Program FilesApache Software FoundationTomcat 5.5web#strutstestweb.xml

2. request. getParameter ("");
ActionForm. getMyFile ();

Method: String filepath = request. getParameter ("myFile ");
Path: D: vssinstallation directory users.txt

Method: String filepath = ActionForm. getMyFile ();
Path: D: vssinstallation directory users.txt

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.