一、工具类代码ResourceLoadUtil.java
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class ResourceLoadUtil {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("ByClazz...\\n--------------------------");
System.out.println(loadResourceByClazz(String.class, "/java/lang/String.class"));
System.out.println(loadResourceByClazz(String.class, "/ResourceLoadUtil.class"));
System.out.println();
System.out.println("ByClasLoader...\\n--------------------------");
System.out.println(loadResourceByClassLoader("java/lang/String.class"));
System.out.println(loadResourceByClassLoader("ResourceLoadUtil.class"));
System.out.println();
System.out.println("sByClasLoader...\\n--------------------------");
System.out.println(loadResourcesByClassLoader("java/lang/String.class"));
System.out.println(loadResourcesByClassLoader("ResourceLoadUtil.class"));
System.out.println(loadResourcesByClassLoader("struts-plugin.xml"));
}
/**
* 根据Class加载资源文件
*
* @param clazz
* @param path
* 以'/'开头的路径: 使用绝对路径寻找文件(以ClassPath为根路径开始)<br>
* 否则: 使用相对路径寻找文件(相对于clazz位置路径开始)
* @return
*/
public static InputStream loadResourceByClazz(Class<?> clazz, String path) {
return clazz.getResourceAsStream(path);
}
/**
* 根据ClassLoader加载资源文件(单个), 使用默认的ClassLoader!
*
* @param path
* 不能以'/'开头的路径
* @return
*/
public static InputStream loadResourceByClassLoader(String path) {
return loadResourceByClassLoader(null, path);
}
/**
* 根据ClassLoader加载资源文件(单个), 使用默认的ClassLoader!
*
* @param path
* 不能以'/'开头的路径
* @return
*/
public static InputStream loadResourceByClassLoader(ClassLoader cl, String path) {
if (cl == null) {
cl = Thread.currentThread().getContextClassLoader();
}
return cl.getResourceAsStream(path);
}
/**
* 根据ClassLoader加载资源文件(多个),使用默认的ClassLoader!
*
* @param path
* 不能以'/'开头的路径
* @return
*/
public static List<URL> loadResourcesByClassLoader(String path) {
return loadResourcesByClassLoader(null, path);
}
/**
* 根据ClassLoader加载资源文件(多个),使用指定的ClassLoader!
*
* @param path
* 不能以'/'开头的路径
* @return
*/
public static List<URL> loadResourcesByClassLoader(ClassLoader cl, String path) {
if (cl == null) {
cl = Thread.currentThread().getContextClassLoader();
}
List<URL> urlLst = new ArrayList<URL>();
try {
Enumeration<URL> urlsEnum = cl.getResources(path);
while (urlsEnum.hasMoreElements()) {
URL url = (URL) urlsEnum.nextElement();
urlLst.add(url);
}
} catch (IOException e) {
e.printStackTrace();
}
return urlLst;
}
}
|
二、执行结果
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
ByClazz...
--------------------------
sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@11cfb549
java.io.BufferedInputStream@a422ede
ByClasLoader...
--------------------------
sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@7f12f614
java.io.BufferedInputStream@35d9dc39
sByClasLoader...
--------------------------
[jar:file:/D:/Java/jdk1.6.0_25/jre/lib/rt.jar!/java/lang/String.class]
[file:/E:/workspace/JBDS/Spring+Struts2-Integration/target/classes/ResourceLoadUtil.class]
[jar:file:/E:/360data/%e9%87%8d%e8%a6%81%e6%95%b0%e6%8d%ae/.m2/repository/org/apache/struts/struts2-spring-plugin/2.3.4.1/struts2-spring-plugin-2.3.4.1.jar!/struts-plugin.xml, jar:file:/E:/360data/%e9%87%8d%e8%a6%81%e6%95%b0%e6%8d%ae/.m2/repository/org/apache/struts/struts2-config-browser-plugin/2.3.4.1/struts2-config-browser-plugin-2.3.4.1.jar!/struts-plugin.xml]
|
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://feticoolo.iteye.com/blog/1695190
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 94
-
2025-05-25 102
-
2025-05-26 102
-
2025-05-25 29
-
2025-05-29 105
热门评论

