PatternsRequestCondition
private String getMatchingPattern(String pattern, String lookupPath) {
if (pattern.equals(lookupPath)) {
return pattern;
}
//判断前缀索引
if (this.useSuffixPatternMatch) {
//是否配置了扩展符以及当前URL是否是以”.“结尾
if (!this.fileExtensions.isEmpty() && lookupPath.indexOf('.') != -1) {
for (String extension : this.fileExtensions) {
//调用自定义的AntPathMatcher 进行匹配
if (this.pathMatcher.match(pattern + extension, lookupPath)) {
return pattern + extension;
}
}
}
else {
boolean hasSuffix = pattern.indexOf('.') != -1;
//判断匹配模式是否以”.*“为结尾
if (!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {
return pattern + ".*";
}
}
}
if (this.pathMatcher.match(pattern, lookupPath)) {
return pattern;
}
//判断是否是 斜线匹配
if (this.useTrailingSlashMatch) {
if (!pattern.endsWith("/") && this.pathMatcher.match(pattern + "/", lookupPath)) {
return pattern +"/";
}
}
return null;
}
/**
* 通常情况下resources目录下的文件打包后存在于类路径下。
* 包路径下如果加入文本文件,打包后在对应的类路径下是无法找到的。即不会打包对应路径下文本文件。
*
* getResource & getResourceAsStream:加载路径其实都是一样的,只不过结果一个表示URL,另一个为InputStream
*
*/
public class CustomResource {
public static void main(String[] args) throws IOException {
m5();
}
/**
* 该方法等价于m4功能
* @throws IOException
*/
public static void m5() throws IOException {
InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream("test.txt");
read(systemResourceAsStream);
URL systemResource = ClassLoader.getSystemResource("custom/custom.txt");
read(systemResource.openStream());
}
/**
* 通过类加载器加载资源是禁止使用"/"。
* @throws IOException
*/
public static void m4() throws IOException {
URL resource = CustomResource.class.getClassLoader().getResource("test.txt");
read(resource.openStream());
//返回为null
System.out.println(CustomResource.class.getClassLoader().getResource("/"));
URL resource1 = CustomResource.class.getClassLoader().getResource("custom/custom.txt");
read(resource1.openStream());
InputStream resourceAsStream = CustomResource.class.getClassLoader().getResourceAsStream("custom/custom.txt");
read(resourceAsStream);
}
/**
* 对用的查找路径同样为类路径的根路径以及当前类的路径下。
* @throws IOException
*/
public static void m3() throws IOException {
//根路径
InputStream resourceAsStream = CustomResource.class.getResourceAsStream("/1.txt");
//对应类的类路径
InputStream resourceAsStream1 = CustomResource.class.getResourceAsStream("1.txt");
read(resourceAsStream1);
read(resourceAsStream);
}
/**
* target/classes/net/clazzloader/resource 路径下加载文件。
* 前提是将1.txt文件打包后存在于对应路径下。
* @throws IOException
*/
public static void m1() throws IOException {
URL resource = CustomResource.class.getResource("1.txt");
InputStream inputStream = resource.openStream();
read(inputStream);
}
/**
* 类路径的根路径,即target/classes。文本文件打包后的路径位置为target/classes/1.txt
* @throws IOException
*/
public static void m2() throws IOException {
URL resource = CustomResource.class.getResource("/1.txt");
InputStream inputStream = resource.openStream();
read(inputStream);
}
public static void read(InputStream inputStream) throws IOException {
StringBuilder rtn = new StringBuilder();
int read = 0;
while ((read = inputStream.read()) != -1){
rtn.append((char)read);
}
inputStream.close();
System.out.println(new String(rtn.toString()));
}
}
JdkTimer.class.getClassLoader().getResourceAsStream("net/csdn/timer/quartz.properties")
如上方式通过类加载器加载指定文件:
- 路径不能加绝对路径“/”。
- 相对路径是指类路径:classpath,而非当前类所在的路径。所以必须在classpath路径下新建路径“net/csdn/timer”,而不是在类JdkTimer同包下新建quartz.properties文件。