The difference between the relative path and the absolute path, Java gets the project Access path method

Source: Internet
Author: User

The difference between a relative path and an absolute path is that the concept of absolute and relative paths is involved in the HTML whenever a file is involved (such as hyperlinks, images, etc.). 1Absolute path Absolute path refers to the path that the file really exists on the hard disk. For example "bg.jpg" This picture is stored in the hard disk "E:\book\ page layout code \ 2nd Chapter" directory, then "bg.jpg" the absolute path of this picture is "E:\book\ page cloth \ Code \ 2nd Chapter \bg.jpg". Then you should use the following statement if you want to specify a background picture of a Web page using an absolute path:<body backround="E:\book\ page layout \ Code \ 2nd Chapter \bg.jpg">2the disadvantage of using absolute paths in fact, in the Web page programming, rarely use absolute path, if you use "E:\book\ page cloth \ Code \ 2nd Chapter \bg.jpg" To specify the location of the background picture, browsing on their own computer may be normal, However, uploading to the Web server will most likely not show the image. Since uploading to a Web server, it is possible that the entire Web site does not have an E drive on the Web server, possibly a D or H drive.   Even if placed in the Web server e-disk, the Web server e-disk will not necessarily exist "E:\book\ page layout \ Code \ 2nd Chapter" This directory, so when browsing the Web page will not display the picture. 3relative paths in order to avoid this occurrence, it is common to select a relative path when specifying a file in a Web page. The so-called relative path is relative to its own target file location. For example, the "s1.htm" file refers to the "bg.jpg" image, because the "bg.jpg" image is in the same directory as the "s1.htm", then use the following code in the "s1.htm" file, as long as the relative position of the two files is unchanged (       That is, in the same directory), you can display the image correctly in the browser regardless of where you upload it to the Web server. <body background="bg.jpg">One more example, assuming that the "s1.htm" file is in the same directory as "E:\book\ page layout \ code \ 2nd", and "bg.jpg" picture is located in the directory of "E:\book\ page layout \ code \ 2nd \img", then "bg.jpg" picture relative to " S1.htm "file, in the" IMG "subdirectory of the directory in which it is located, the statement referring to the picture should be:<body background="img/bg.jpg">Note: The relative path uses the/"character as the delimiter character for the directory, and the absolute path can use the" \ "or"/"character as the delimiter character for the directory. Since the "IMG" Directory is a subdirectory under the "2nd Chapter" directory, there is no need to add "/" Before "IMG""character. Often used in relative paths "./"to represent the previous level of the directory. If there are multiple top-level catalogs, you can use more than one ". /", such as" http://www.cnblogs.com/"represents the upper parent directory. Assuming that the "s1.htm" file is in the same directory as "E:\book\ page layout \ code \ 2nd" and the "bg.jpg" picture is in the same directory as "E:\book\ page layout \ Code", then the "bg.jpg" image is relative to the "s1.htm" file, is in the parent directory of the directory in which it is located, the statement referring to the picture should be:<body background=".. /bg.jpg">One more example, assuming that the "s1.htm" file is in the same directory as "E:\book\ page layout \ code \ 2nd", and "bg.jpg" picture is located in the directory "E:\book\ page layout \ Code \img", then "bg.jpg" picture relative to "s1.htm "The file is in the" IMG "subdirectory in the parent directory of the directory in which it is located, the statement referring to the picture should be:<body background=".. /img/bg.jpg">4Relative virtual Directories There is also a special representation of relative paths: "Relative virtual Directories". Take a look at the following example:<body background="/img/bg.jpg">in this example, the value of the Background property is "/img/bg.jpg ", note that there is a"/"character before" IMG ". This "/" represents the root directory of the virtual directory. Assuming that the "E:\book\ page layout \ Code" is set to the virtual directory, then "/img/bg.jpg" The Real path is "E:\book\ page layout \ Code \img \bg.jpg", if the "E:\book\ page layout \ Code \ 2nd" Set as a virtual directory, then "/img/The true path to bg.jpg "E:\book\ page layout \ code \ 2nd \img\bg.jpg" is different from the relative path that is called in the JSP and class files. In the JSP, the root directory is webroot in the class file, the root directory is Webroot/web-inf/classes Of course you can also use System.getproperty ("User.dir"get the absolute path to your project. 1get the path in the. JSP: Take the project name test as an example: (1) Gets the full path of the current page that contains the project name: Request.getrequesturi () Result:/test/test.jsp (2) Get project name: Request.getcontextpath () Result:/TEST (3get the current page in the same directory as the full name: Request.getservletpath () Result: If the page is in the JSP directory/test/jsp/test.jsp (4) Gets the full path of the server on which the page is located: Application.getrealpath ("page. JSP") Results: D:\resin\webapps\TEST\test.jsp (5) Gets the absolute path of the server on which the page is located: abspath=NewJava.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent (); Results: D:\resin\webapps\TEST2. Get the path in the class: (1) The absolute path of the class: class.class. GetClass (). GetResource ("/"). GetPath () Results:/d:/test/webroot/web-inf/classes/pack/ (2) Get the project path: System.getproperty ("User.dir") Results: D:\TEST3get the path in the servlet: (1) Get the Project catalog: Request.getsession (). Getservletcontext (). Getrealpath ("") parameters can be specific to the package name. Results: E:\Tomcat\webapps\TEST (2) Get IE Address bar address: Request.getrequesturl () Results: http://localhost:8080/test/test(3) Get relative Address: Request.getrequesturi () Results:/test/test Another, class class has a getResourceAsStream method, remember that there was a project to read in the same package of XML, the use of this. 1how to get the current file path commonly used: (1). Test.class. GetResource ("") Gets the URI directory of the current class Filetest.class file. Don't include yourself! (2). Test.class. GetResource ("/") Gets the absolute URI of the current classpath. (3). Thread.CurrentThread (). Getcontextclassloader (). GetResource ("") is also the absolute URI of the current classpath. (4). Test.class. getClassLoader (). GetResource ("") is also the absolute URI of the current classpath. (5). Classloader.getsystemresource ("") is also the absolute URI of the current classpath. Try not to use relative System.getproperty ("User.dir") The relative path of the current user directory, which can be seen in a variety of results. (6)NewFile (""). GetAbsolutePath () is also available. Note: If spaces are present in these paths and the spaces are replaced, you can use the path= Java.net.URLDecoder.decode (Path,"Utf-8") for conversion; 2. Web Server (1). Tomcat outputs System.getproperty in the class ("User.dir"); the%tomcat_home%/is displayed.Bin (2). Resin is not the relative path that your JSP puts, it is the JSP engine that executes this JSP to compile the path of the servlet to the root. For example, test file F with the new document Method=NewFile ("a.htm"); This a.htm is in the installation directory of resin (3). How to read a file using Servletcontext.getresourceasstream () you Can (4). Get the file True path String File_real_path=servletcontext.getrealpath ("Mypath/filename"); It is not recommended to use Request.getrealpath ("/");

The difference between the relative path and the absolute path, Java gets the project Access path method

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.