"Go" Java File path summary

Source: Internet
Author: User

1. Understanding of Basic concepts

Absolute path: The absolute path is the real path on the hard disk of the file or directory on your home page, (URL and physical path) such as:
C:xyz Est.txt represents the absolute path to the Test.txt file. The http://www.sun.com/index.htm also represents a URL absolute path.

Relative path: The path relative to a base directory. Contains the relative path to the web (relative to the directory in HTML), for example:
In a servlet, "/" represents a directory of Web apps. and the relative representation of the physical path. For example: "./" represents the current directory, ". /"represents the parent directory. This similar representation also belongs to the relative path.
For additional information about Uri,url,urn, please refer to RFC documentation standards.

RFC 2396:uniform Resource Identifiers (URI): Generic Syntax,
(Http://www.ietf.org/rfc/rfc2396.txt)


2. About the relative path and absolute path in Jsp/servlet.

2.1 Server-side address

The relative address of the server side refers to the address that is relative to your Web application, which is resolved on the server side (different from the relative address in HTML and JavaScript. They are parsed by the client browser) in other words, the relative addresses in the JSP and servlet should be relative to your Web application, which is relative to http://192.168.0.1/webapp/.

Where they are used are:
Forward:servlet in Request.getrequestdispatcher (address); This address is parsed on the server side, so You want to forward to a.jsp should write: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application WebApp, its absolute address is:/http 192.168.0.1/webapp/user/a.jsp. Sendredirect: <%response.sendredirect ("/rtccp/user/a.jsp") in the JSP;%>

2.22, the address of the client

The relative addresses in all the HTML pages are relative to the root of the server (HTTP://192.168.0.1/), not http://192.168.0.1/webapp/(with the directory of the Web app under the directory). The address of the Action property of the form form in HTML should be relative to the server root directory (HTTP://192.168.0.1/), so if committed to a.jsp: action= "/webapp/user/a.jsp" or action= " <%=request.getcontextpath ()% > "/user/a.jsp;
The actiom= "/webapp/handleservlet" JavaScript that is submitted to the servlet is also parsed on the client, so its relative path is the same as the form form.


Therefore, in general, in the Jsp/html page and other references such as css,javascript.action attributes are best added
<%=request.getcontextpath ()%> To ensure that the referenced files belong to a directory in the Web App. In addition, you should try to avoid the use of similar ".", "./", ". /.. /"Similar relative path relative to the location of the file, so that when the file is moved, it is prone to problems.


3. Get the relative and absolute paths of the current application in Jsp/servlet
Obtaining the relative and absolute paths of the current application in the 3.1 JSP
Absolute path for root directory: Request.getrequesturi ()
Absolute path to File: Application.getrealpath (Request.getrequesturi ());
The absolute path of the current Web application: Application.getrealpath ("/");
Gets the upper-level directory of the requested file: New file (Application.getrealpath (Request.getrequesturi ())). GetParent ()

Obtaining the relative and absolute paths of the current application in the 3.2 servlet
Absolute path for root directory: Request.getservletpath ();
Absolute path to File: Request.getsession (). Getservletcontext (). Getrealpath
(Request.getrequesturi ())
Absolute path to the current web app: Servletconfig.getservletcontext (). Getrealpath ("/");
(ServletContext objects are available in several ways:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
)

Method of obtaining relative path, absolute path in 4.java class
4.1 Obtaining an absolute path in a separate Java class
According to Java.io.File's doc text, we know:
By default, the directory represented by new File ("/") is: System.getproperty ("User.dir").
The program gets the current path of the execution class
Package org.cheng.file;
Import Java.io.File;

public class Filetest {
public static void Main (string[] args) throws Exception {

System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource (""));

System.out.println (FileTest.class.getClassLoader (). GetResource (""));

System.out.println (Classloader.getsystemresource (""));
System.out.println (FileTest.class.getResource (""));
System.out.println (FileTest.class.getResource ("/")); The path of the class file
System.out.println (New File ("/"). GetAbsolutePath ());
System.out.println (System.getproperty ("User.dir"));
}
}

4.2 Java classes in the server get the current path (from the network)
(1). Weblogic

The WebApplication system file root directory is the root directory where your WebLogic installation is located.
For example: If your WebLogic is installed in c:eaweblogic700 .....
So, your file root path is C:.
So there are two ways to get you to access your server-side files:
A. Using an absolute path:
For example, put your parameter file in C:yourconfigyourconf.properties,
Direct use of the new FileInputStream ("Yourconfig/yourconf.properties");
B. Using relative paths:
The root of the relative path is the root path of your webapplication, which is the top-level directory of the Web-inf, placing your parameter file

In Yourwebappyourconfigyourconf.properties,
This is used:
New FileInputStream ("./yourconfig/yourconf.properties");
Both of these methods can be chosen by themselves.

(2). Tomcat

Output System.getproperty ("User.dir") in the class;%tomcat_home%/bin is displayed.

(3). Resin

is not the relative path that your JSP puts, it is the JSP engine that executes this JSP compiled into a servlet
The path to the root. For example, the new file method is used to test for file F = new ("a.htm");
This a.htm is under the resin installation directory.

(4). How to read the relative path?

In a Java file, getresource or getresourceasstream can be

Example: GetClass (). getResourceAsStream (FilePath);//filepath can be "/filename", here/on behalf of the Web

Publish Root Path under Web-inf/classes

The default path for using this method is: web-inf/classes. has been tested in Tomcat.

5. Relative path when reading files, avoid hard coding and absolute path use. (from the network)
5.1 Use spring's di mechanism to get the file and avoid hard coding.
Refer to the following connection content:
http://www.javajia.net/viewtopic.php?p=90213&
5.2 Reading of configuration files
Refer to the following connection content:
Http://dev.csdn.net/develop/article/39/39681.shtm
5.3 Read an XML file from a virtual path or relative path, avoiding hard-coded
Refer to the following connection content:
Http://club.gamvan.com/club/clubPage.jsp?iPage=1&tID=10708&ccID=8

Common operations for files in 6.Java (copy, move, delete, create, etc.) (from the network)
Common Java File Operation classes
Http://www.easydone.cn/014/200604022353065155.htm

Java File Operations Encyclopedia (in JSP)
Http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html

Java file operation details (Java Chinese network)
Http://www.51cto.com/html/2005/1108/10947.htm

JAVA How to create a delete modify copy directory and file
Http://www.gamvan.com/developer/java/2005/2/264.html

Summarize:
Through the use of the above content can be resolved on the Web application server side, move files, find files, copy
Delete files and other operations, while the relative address of the server, the concept of absolute address clearer.
Recommended reference URI, RFC standard text block. At the same time, Java.io.File. Java.net.URI and other content to understand thoroughly
Other aspects of the understanding can be more thorough and thorough.

"Go" Java File path summary

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.