详解Java读取Jar中资源文件及示例代码
作者:lqh 时间:2021-07-12 11:18:52
详解Java读取Jar中资源文件及实现代码
直接上代码,文章的注释部分说的比较清楚,大家可以参考下,
工具类源代码: ResourceLoadFromJarUtil.java
实现代码:
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.commons.io.IOUtils;
public class ResourceLoadFromJarUtil {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
/* 资源文件路径,不能以'/'字符字符开头 */
String resourcePath = "struts-plugin.xml";
/* 获取ClassPath下的所有jar路径 */
String[] cps = System.getProperty("java.class.path").split(";");
/* 读取本地Jar文件 */
for (String cp : cps) {
if (!cp.endsWith(".jar")) {
continue;
}
InputStream in = loadResourceFromJarFile(cp, resourcePath);
if (in != null) {
System.err.println(IOUtils.toString(in));
in.close();
}
}
/* 读取本地Jar文件 */
for (String cp : cps) {
if (!cp.endsWith(".jar")) {
continue;
}
InputStream in = loadResourceFromJarURL(cp, resourcePath);
if (in != null) {
System.err.println(IOUtils.toString(in));
in.close();
}
}
/* 读取网络Jar文件 */
InputStream in = loadResourceFromJarURL(
"http://localhost:8080/SpringStruts2Integration/struts2-spring-plugin-2.3.4.1.jar", resourcePath);
if (in != null) {
System.err.println(IOUtils.toString(in));
in.close();
}
}
/**
* 读取Jar文件中的资源
*
* @param jarPath
* 本地jar文件路径
* @param resPath
* 资源文件所在jar中的路径(不能以'/'字符开头)
* @return 如果资源加载失败,返回null
*/
public static InputStream loadResourceFromJarFile(String jarPath, String resPath) {
if (!jarPath.endsWith(".jar")) {
return null;
}
try {
JarFile jarFile = new JarFile(jarPath);
JarEntry jarEntry = jarFile.getJarEntry(resPath);
if (jarEntry == null) {
return null;
}
return jarFile.getInputStream(jarEntry);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 读取Jar文件中的资源
*
* @param jarUrl
* 本地jar文件或网络上(ttp://host:port/subpath/jarfile.jar)jar文件路径
* @param resPath
* 资源文件所在jar中的路径(不能以'/'字符开头)
* @return 如果资源加载失败,返回null
*/
public static InputStream loadResourceFromJarURL(String jarUrl, String resPath) {
if (!jarUrl.endsWith(".jar")) {
return null;
}
URL url = null;
if (jarUrl.startsWith("http://")) {
try {
url = new URL("jar:" + jarUrl + "!/");
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
} else {
try {
url = new URL("jar:file:/" + jarUrl + "!/");
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
}
try {
JarURLConnection jarURLConnection;
jarURLConnection = (JarURLConnection) url.openConnection();
JarFile jarFile = jarURLConnection.getJarFile();
JarEntry jarEntry = jarFile.getJarEntry(resPath);
if (jarEntry == null) {
return null;
}
return jarFile.getInputStream(jarEntry);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
来源:http://feticoolo.iteye.com/blog/1696331
标签:java,jar,资源
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一文搞懂Java创建线程的五种方法
2023-10-30 18:35:04
![](https://img.aspxhome.com/file/2023/3/58693_0s.png)
挑战4道Java试题
2022-04-12 04:41:02
Java 开启多线程常见的4种方法
2023-11-23 02:30:10
![](https://img.aspxhome.com/file/2023/0/58580_0s.png)
Java如何使用ReentrantLock实现长轮询
2023-11-26 00:09:08
Java String类的理解及字符串常量池介绍
2022-11-14 15:42:22
![](https://img.aspxhome.com/file/2023/4/66934_0s.png)
Java中静态代码块、构造代码块、构造函数和普通代码块的区别
2023-11-25 10:09:06
![](https://img.aspxhome.com/file/2023/8/60158_0s.png)
flutter实现appbar下选项卡切换
2023-06-21 13:35:24
![](https://img.aspxhome.com/file/2023/3/87313_0s.gif)
Java8新特性:函数式编程
2021-12-01 03:09:02
SpringCloud如何解决服务之间的通信问题
2023-03-27 03:13:05
![](https://img.aspxhome.com/file/2023/2/72552_0s.jpg)
Spring AOP实现打印HTTP接口出入参日志
2021-10-09 13:38:37
![](https://img.aspxhome.com/file/2023/1/78371_0s.png)
java中怎样表示圆周率
2022-11-30 11:20:00
![](https://img.aspxhome.com/file/2023/8/79848_0s.png)
Java中Arraylist动态扩容方法详解
2023-04-30 23:25:53
![](https://img.aspxhome.com/file/2023/6/82026_0s.jpg)
解决idea爆红 cant resolve symbol String的问题解析
2023-10-11 10:47:30
![](https://img.aspxhome.com/file/2023/5/58615_0s.png)
c# 在windows中操作IIS设置FTP服务器的示例
2023-07-18 06:13:01
![](https://img.aspxhome.com/file/2023/6/66116_0s.jpg)
学习Java之如何正确地跳出循环结构
2021-10-21 11:21:53
![](https://img.aspxhome.com/file/2023/4/78244_0s.webp)
Java异常简介和架构_动力节点Java学院整理
2022-09-03 07:07:52
![](https://img.aspxhome.com/file/2023/8/77958_0s.jpg)
C# 如何设置label(标签)控件的背景颜色为透明
2023-05-27 11:03:00
![](https://img.aspxhome.com/file/2023/2/79042_0s.jpg)
详解Mybatis通用Mapper介绍与使用
2023-11-29 08:49:08
SpringCloud OpenFeign超详细讲解模板化远程通信的实现
2023-04-27 06:43:29
springboot schedule 解决定时任务不执行的问题
2021-05-30 05:00:38