A solution for Java relative paths

Source: Internet
Author: User
Tags getstream

For web development, you sometimes need to access local files (for example, the configuration file placed under Web-Info). However, this is a simple task, because of the different loading mechanisms of Tomcat and WebLogic, the platform cannot be implemented, although httpservletrequest and request can be used. getsession (). getservletcontext (). getrealpath (""), but in some places there is no httpservletrequest object, although other means such as pre-loading, save the Application Path to the global variable in advance, but still not a good solution, xxxx. class. getclassloader (). getresource ("") seems to be a better solution, but later I found that Weblogic is under user_projects/domains/XXX, rather than under Web-info/classes, because the path of the Class Loader of the application is different from that of Tomcat. it seems that only XXXX is used. class. getresource (). In this way, the resource to be searched must be in the same path as the class. Generally, the class has a package path such as COM. xx we generally expect the resource file to be placed under Web-info/classes, so we need a class to parse the relative path. for example .. /...

For example, obtain the resourceloader. getresource ("../Web. xml") of the web. xml file ");

Resourceloader. getresource (".../../XXX"); // obtain the xxx directory under the web directory

Resourceloader. getresource ("A. Text"); // obtain the web-info/classes/a. Text

 

Public class resourceloader {
Private resourceloader (){
}

/**
*
* Description: used to load the resource files under the classpath path of the class. You can use the ../symbol to load resources outside the classpath.
*
* @ Param relativepath
* Equivalent path
* @ Return URL object
*/
Public static URL getresource (string relativepath ){
URL resourceabsoluteurl = NULL;
Try {
Relativepath = getstringfornum (resourceloader. Class. getname ()
. Split ("/."). Length-1 ,"../")
+ Relativepath;
If (relativepath. indexof ("../") <0 ){
Return resourceloader. Class. getresource (relativepath );
}
String classpath = resourceloader. Class. getresource (""). tostring ();
If (relativepath. substring (0, 1). Equals ("/")){
Relativepath = relativepath. substring (1 );
}
String wildcardstring = relativepath. substring (0, relativepath
. Lastindexof (".../") + 3 );
Relativepath = relativepath. substring (relativepath
. Lastindexof (".../") + 3 );
Int containsum = containsum (wildcardstring ,"../");
Classpath = cutlaststring (classpath, "/", containsum );
String resourceabsolutepath = classpath + relativepath;
Resourceabsoluteurl = new URL (resourceabsolutepath );
} Catch (exception e ){
E. printstacktrace ();
}
Return resourceabsoluteurl;
}

Public file getresourcefile (string relativepath ){
Try {
Return new file (getresource (relativepath). GetFile ());
} Catch (exception e ){
Return NULL;
}
}

/**
* Obtain the resource file under the classpath of the class. You can use the ../symbol to load resources outside the classpath.
*
* @ Param relativepath
* Equivalent path
* @ Return input stream
*/
Public static inputstream getstream (string relativepath ){
Try {
Return getstream (getresource (relativepath ));
} Catch (exception e ){
E. printstacktrace ();
Return NULL;
}
}

/**
*
* Description: gets the properties file under the classpath of the class. You can use the ../symbol to load resources outside the classpath.
*
* @ Param Resource
* Equivalent path
* @ Return properties object
*/
Public static properties getproperties (string resource ){
Properties Properties = new properties ();
Inputstream in = NULL;
Try {
In = getstream (Resource );
Properties. Load (in );
Return properties;
} Catch (exception e ){
E. printstacktrace ();
Return NULL;
} Finally {
Try {
In. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}

/*************************************** ************************************
*
*/** Calculate the number of DeST contained in the string source.
*
* @ Param Source
* @ Param dest
* @ Return source contains the number of dest
*/
Private Static int containsum (string source, string DEST ){
Int containsum = 0;
Int destlength = DeST. Length ();
While (source. indexof (DEST)> = 0 ){
Containsum = containsum + 1;
Source = source. substring (destlength );
}
Return containsum;
}

/**
*
* Description: gets a stream through a URL.
*
* @ Param URL
* @ Return
* @ Throws ioexception
*/
Private Static inputstream getstream (URL ){
Try {
If (URL! = NULL)
Return URL. openstream ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Return NULL;
}

/**
* Remove the num string DEST from the back of the source string
*
* @ Param Source
* @ Param dest
* @ Param num
* @ Return
*/
Private Static string cutlaststring (string source, string DEST, int num ){
For (INT I = 0; I <num; I ++)
Source = source. substring (0, source. lastindexof (DEST, Source
. Length ()-2) + 1 );
Return source;
}

/**
*
* Description: Concatenates the specified string 'str' for num.
*
* @ Param num
* @ Param Str
* @ Return
*/
Private Static string getstringfornum (INT num, string Str ){
String ret = "";
For (; num> 0; num --)
RET + = STR;
Return ret;
}

Public static void main (string [] ARGs ){
System. Out. println (resourceloader. getresource ("../Web. xml "));
}
}

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.