在项目中使用Maven,有大量的jar包。
原来代码中 直接使用 Xxx.class.getResourceAsStream()方法来获取资源。
问题出现了,一旦classpath下或其它先加载jar包中出现相同的配置文件,那么就会优先读取这些文件
下面这个工具类可以帮助我们处理jar包内的资源
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Enumeration;
import com.zskx.pem.network.biz.commons.helper.file.FileReader;
import com.zskx.web.commons.action.BaseAction;
/**
* Jar包相关工具
*
* @author <a href="mailto:gaollg@sina.com">gaollg@sina.com</a>
*
*/
public class JarUtils {
/**
* 取得class所在 jar包名称
*
* @param clazz
* @return 返回值为null 表示直接在classpath下
*/
public static String getJarName(Class<?> jarClazz){
String file = jarClazz.getProtectionDomain().getCodeSource().getLocation().getFile();
if(file.endsWith(File.separator)){
//就在classpath下
return null;
}
return new File(file).getName();
}
/**
* 取得class所在 jar包 中的资源
*
* @param jarClazz
* @param resource 所取的资源
* @return
*/
public static URL getResourceFromJar(Class<?> jarClazz, String resource){
if(resource.startsWith(File.separator)){
resource = resource.substring(1);
}
URL classDir = jarClazz.getProtectionDomain().getCodeSource().getLocation();
if(classDir.getPath().endsWith(File.separator)){
//就在classpath下
return jarClazz.getResource(resource);
}
//jar文件
String path = classDir.toString();
try {
Enumeration<URL> res = jarClazz.getClassLoader().getResources(resource);
while (res.hasMoreElements()) {
URL url = res.nextElement();
if(url.getPath().startsWith(path)){
return url;
}
}
} catch (IOException e) {
//IO异常直接返回 null
return null;
}
return null;
}
/**
* 取得class所在 jar包 中的资源
*
* @param jarClazz
* @param resource 所取的资源
* @return
*/
public static InputStream getResourceAsStreamFromJar(Class<?> jarClazz, String resource){
URL url = getResourceFromJar(jarClazz, resource);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
/**
* 取得文件的所有文本内容
* @param file
* @return
*/
public static String readAllText(InputStream in){
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
try {
String tempString = null;
while ((tempString = br.readLine()) != null) {
sb.append(tempString).append("\r\n");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
public static String readInputStream(Charset charset, InputStream in) {
byte[] bytes = new byte[1024];
int length = -1;
ByteArrayOutputStream byteOutput= new ByteArrayOutputStream();
try {
while ((length = in.read(bytes)) != -1) {
byteOutput.write(bytes, 0, length);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return new String(byteOutput.toByteArray(), charset);
}
public static String readResourceFromJar(Charset charset, Class<?> jarClazz, String resource) {
InputStream in = getResourceAsStreamFromJar(jarClazz, resource);
return readInputStream(charset, in);
}
public static String readResourceFromJar(Class<?> jarClazz, String resource) {
return readResourceFromJar(Charset.forName("utf-8"), jarClazz, resource);
}
public static void main(String[] args) {
//测试类 BaseAction在jar包中
System.out.println(readResourceFromJar(BaseAction.class, "image.properties"));
}
}