Code for retrieving paths in java/struts/tomcat

Source: Internet
Author: User
The code is as follows: Copy code
File f = new File (".");
String absolutePath = f. getAbsolutePath ();
System. out. println (absolutePath );

1. Space in the path:

The code is as follows: Copy code

String pathtem = null;
URL ut = Thread. currentThread (). getContextClassLoader (). getResource ("");
Pathtem = ut. toString ();
Pathtem = pathtem. replace ("% 20 & Prime;," "); // remove spaces
Pathtem = pathtem. substring (6, pathtem. length (); // remove "file :/"
Return value:
D:/apache/webapps/Ev/WEB-INF/classes/

Obtain the path in struts
String rootuploadpath = this. getServlet (). getServletContext (). getRealPath ("/");

2. After the jar package is generated

The code is as follows: Copy code
String pathtem = null;
URL ut = Csssb_se.class.getResource ("");
Pathtem = ut. toString ();
Pathtem = pathtem. replace ("% 20 & Prime;," ");
Pathtem = pathtem. substring (0, pathtem. indexOf (projectname ));
Pathtem = pathtem. replace ("file :///","");
Pathtem = pathtem. replace ("file :/","");
Pathtem = pathtem. replace ("jar :","");

3. After the war package is generated, Tomcat

The code is as follows: Copy code

String Dirpath = null;
String Project_name = "TranData ";
URL ut = Thread. currentThread (). getContextClassLoader (). getResource ("");
Dirpath = ut. toString ();
Dirpath = Dirpath. replace ("% 20 & Prime;," ");
Dirpath = Dirpath. substring (6, Dirpath. length ());
Int project_name_index = Dirpath. indexOf (Project_name );
Dirpath = Dirpath. substring (0, project_name_index
+ Project_name.length () + 1 );

String xmlfilepath = Dirpath + "s. xml ";

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.