本文摘机自 [url=http://download-llnw.oracle.com/javase/1.4.2/docs/guide/resources/resources.html][size=large]Accessing Resources in a Location-Independent Manner[/size][/url]
tag: java,resource,location independent
[size=large]Resource:[/size]
/ separated string
/<shortName>.<extension>
[size=large]System Resource:[/size]
CLASSPATH
A resource may be found in a different entry in CLASSPATH than where the class is loaded
[size=large]Class Loader:[/size]
all class loaders search for a resource as system resource firstly
AppletClassLoader - system resource > JAR > CODEBASE > remote
[size=large]Resouce Name:[/size]
replace "." with "/" in fully qualified package name
and add resource name as "<name>.<ext>"
absolute name starts with "/", otherwise it is relative
[size=large]Manipulating Resource:[/size]
return null when not exist or permission denied
use URL.openStream to open the returned URL
use (ImageProducer)URL.getContent to get image stored in the Resource
[size=large]Security:[/size]
return null when in security context addresses existence attacks
ClassLoader will not provide access to the contents of a .class file
no restriction regarding non-system class loader found resource
resource [b]in directory[/b] - getResource() use File.exists() which use checkRead() in the security manager
resource [b]in JAR[/b] - succeed for all files regardless of system or non-system class
tag: java,resource,location independent
[size=large]Resource:[/size]
/ separated string
/<shortName>.<extension>
[size=large]System Resource:[/size]
CLASSPATH
A resource may be found in a different entry in CLASSPATH than where the class is loaded
[size=large]Class Loader:[/size]
all class loaders search for a resource as system resource firstly
AppletClassLoader - system resource > JAR > CODEBASE > remote
[size=large]Resouce Name:[/size]
replace "." with "/" in fully qualified package name
and add resource name as "<name>.<ext>"
absolute name starts with "/", otherwise it is relative
[size=large]Manipulating Resource:[/size]
return null when not exist or permission denied
use URL.openStream to open the returned URL
use (ImageProducer)URL.getContent to get image stored in the Resource
[size=large]Security:[/size]
return null when in security context addresses existence attacks
ClassLoader will not provide access to the contents of a .class file
no restriction regarding non-system class loader found resource
resource [b]in directory[/b] - getResource() use File.exists() which use checkRead() in the security manager
resource [b]in JAR[/b] - succeed for all files regardless of system or non-system class