package com.caakee.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*@author 沈东良 shendl_s@hotmail.com
*Nov29,2006 10:34:34AM
*用来加载类,classpath下的资源文件,属性文件等。
*getExtendResource(StringrelativePath)方法,可以使用../符号来加载classpath外部的资源。
*/
public class ClassLoaderUtil {
private static Log log=LogFactory.getLog(ClassLoaderUtil.class);
/**
*Thread.currentThread().getContextClassLoader().getResource("")
*/
/**
*加载Java类。 使用全限定类名
*@paramclassName
*@return
*/
public static Class loadClass(String className) {
try {
return getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException("class not found '"+className+"'", e);
}
}
/**
*得到类加载器
*@return
*/
public static ClassLoader getClassLoader() {
return ClassLoaderUtil.class.getClassLoader();
}
/**
*提供相对于classpath的资源路径,返回文件的输入流
*@param relativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
*@return 文件输入流
*@throws IOException
*@throws MalformedURLException
*/
public static InputStream getStream(String relativePath) throws MalformedURLException, IOException {
if(!relativePath.contains("../")){
return getClassLoader().getResourceAsStream(relativePath);
}else{
return ClassLoaderUtil.getStreamByExtendResource(relativePath);
}
}
/**
*
*@param url
*@return
*@throwsIOException
*/
public static InputStream getStream(URL url) throws IOException{
if(url!=null){
return url.openStream();
}else{
return null;
}
}
/**
*
*@param relativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
*@return
*@throws MalformedURLException
*@throws IOException
*/
public static InputStream getStreamByExtendResource(String relativePath) throws MalformedURLException, IOException{
return ClassLoaderUtil.getStream(ClassLoaderUtil.getExtendResource(relativePath));
}
/**
*提供相对于classpath的资源路径,返回属性对象,它是一个散列表
*@paramresource
*@return
*/
public static Properties getProperties(String resource) {
Properties properties = new Properties();
try {
properties.load(getStream(resource));
} catch (IOException e) {
throw new RuntimeException("couldn't load properties file '"+resource+"'", e);
}
return properties;
}
/**
*得到本Class所在的ClassLoader的Classpat的绝对路径。
*URL形式的
*@return
*/
public static String getAbsolutePathOfClassLoaderClassPath(){
ClassLoaderUtil.log.info(ClassLoaderUtil.getClassLoader().getResource("").toString());
return ClassLoaderUtil.getClassLoader().getResource("").toString();
}
/**
*
*@param relativePath 必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
*@return 资源的绝对URL
*@throws MalformedURLException
*/
public static URL getExtendResource(String relativePath) throws MalformedURLException{
ClassLoaderUtil.log.info("传入的相对路径:"+relativePath) ;
//ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../"))) ;
if(!relativePath.contains("../")){
return ClassLoaderUtil.getResource(relativePath);
}
String classPathAbsolutePath=ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath();
if(relativePath.substring(0, 1).equals("/")){
relativePath=relativePath.substring(1);
}
ClassLoaderUtil.log.info(Integer.valueOf(relativePath.lastIndexOf("../"))) ;
String wildcardString=relativePath.substring(0,relativePath.lastIndexOf("../")+3);//类似于../../../../
relativePath=relativePath.substring(relativePath.lastIndexOf("../")+3);//相对于路径部分
int containSum=ClassLoaderUtil.containSum(wildcardString, "../");//总共向上几级
classPathAbsolutePath= ClassLoaderUtil.cutLastString(classPathAbsolutePath, "/", containSum);
String resourceAbsolutePath=classPathAbsolutePath+relativePath;
ClassLoaderUtil.log.info("绝对路径:"+resourceAbsolutePath) ;
URL resourceAbsoluteURL = new URL(resourceAbsolutePath);
return resourceAbsoluteURL;
}
/**
*
*@param source
*@param dest
*@return
*/
private static int containSum(String source,String dest){
int containSum=0;
int destLength=dest.length();
while(source.contains(dest)){
containSum=containSum+1;
source=source.substring(destLength);
}
return containSum;
}
/**
*
*@param source
*@param dest
*@param num
*@return
*/
private static String cutLastString(String source,String dest,int num){
// String cutSource=null;
for(int i=0;i<=num;i++){
//source=source.substring(0, source.lastIndexOf(dest, source.length()-2)+1);
source=source.substring(0, source.lastIndexOf(dest));
}
return source+File.separator;
}
/**
*
*@paramre source
*@return
*/
public static URL getResource(String resource){
ClassLoaderUtil.log.info("传入的相对于classpath的路径:"+resource) ;
return ClassLoaderUtil.getClassLoader().getResource(resource);
}
/**
*@param args
*@throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException {
//ClassLoaderUtil.getExtendResource("../spring/dao.xml");
//ClassLoaderUtil.getExtendResource("../../../src/log4j.properties");
ClassLoaderUtil.getExtendResource("log4j.properties");
System.out.println(ClassLoaderUtil.getClassLoader().getResource("log4j.properties").toString());
}
}
分享到:
相关推荐
一个简单的ClassLoader,用于加载指定位置的字节码文件
jar包,官方版本,自测可用
文中指出尽量不要使用相对于System.getProperty(\\\"user....使用上面 ClassLoaderUtil类的public static URL getExtendResource(String relativePath)方法已经能够使用相对于classpath的相对路径定位所有位置的资源。
浏览器通用工具类ClassLoaderUtil ----->类加载通用工具类ClassUtil ----->类通用工具类CollectionUtil ----->依赖与commons.lang集合通用工具类DateUtil ----->依赖与commons.lang的日期通用工具类...
数据库实验.py
1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
学会学习心理课拒绝诱惑:自制力培养手册.docx
基于matlab+Simulink模拟的微电网系统包括包括电源、电力电子设备等+源码+开发文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 项目简介: 这是一个完整的微电网模型,包括电源、电力电子设备、使用MatLab和Simulink的负载和电源模型。该模型基于费萨尔·穆罕默德的硕士论文《微网格建模与仿真》。 什么是微电网 模拟的微电网使用一组电源和负载在与任何集中式电网(宏电网)断开连接的情况下工作,并自主运行,为其局部区域提供电力。该仿真对微电网在稳态下进行建模,以分析其对输入变化的瞬态响应。 此模拟的目的 对系统进行全年模拟,测量负载、产量、电压和频率。 给出简化规划和资源评估阶段的方法。
Unity插件 Translucent Image 可帮助你构建精美的模糊背景 UI,例如在 iOS/MacOS/Windows 10 Fluent 设计中的 UI。 与许多其他背景模糊解决方案不同,Translucent Image 采用一种对性能影响最小的高效算法,因此用户可以享受更高的帧速率和更长的电池寿命。不仅如此,当你将模糊调高时,它还可以产生完美的平滑效果,而其它资源在高度模糊时会呈现难看的块状图像。
基于卷积神经网络的人脸识别卷积神经网络(Convolutional Neural Networks, CNNs 或 ConvNets)是一类深度神经网络,特别擅长处理图像相关的机器学习和深度学习任务。它们的名称来源于网络中使用了一种叫做卷积的数学运算。以下是卷积神经网络的一些关键组件和特性: 卷积层(Convolutional Layer): 卷积层是CNN的核心组件。它们通过一组可学习的滤波器(或称为卷积核、卷积器)在输入图像(或上一层的输出特征图)上滑动来工作。 滤波器和图像之间的卷积操作生成输出特征图,该特征图反映了滤波器所捕捉的局部图像特性(如边缘、角点等)。 通过使用多个滤波器,卷积层可以提取输入图像中的多种特征。 激活函数(Activation Function): 在卷积操作之后,通常会应用一个激活函数(如ReLU、Sigmoid或tanh)来增加网络的非线性。 池化层(Pooling Layer): 池化层通常位于卷积层之后,用于降低特征图的维度(空间尺寸),减少计算量和参数数量,同时保持特征的空间层次结构。 常见的池化操作包括最大池化(Max Pooling)和平均
基于java进行的软件测试实验代码.zip
【优化求解】遗传算法求解多城市多应急物流中心选址问题【含Matlab源码 1724期】.zip
结构型设计模式(7种)
课设毕设基于SpringBoot+Vue的旧物置换网站 LW+PPT+源码可运行.zip
健康菜谱App是一款专为追求健康饮食生活的用户设计的应用程序。它提供了一系列精心挑选的营养食谱,旨在帮助用户做出美味又健康的餐点。以下是健康菜谱App的核心特点: 丰富食谱库:包含数百种健康食谱,涵盖早餐、午餐、晚餐及小吃等。 营养信息标注:每道食谱都配有详细的营养信息,包括卡路里、蛋白质等。 个性化推荐:根据用户的饮食习惯和健康目标,智能推荐合适的菜谱。 食材替换建议:提供食材替换选项,帮助用户根据自己的口味和需求调整食谱。 一键购物清单:自动生成购物清单,方便用户购买所需食材。 步骤图解:每道食谱都配有清晰的步骤图解,即使是烹饪新手也能轻松上手。 社区分享功能:用户可以在社区中分享自己的烹饪成果,交流烹饪心得。 无广告干扰:提供无广告的用户体验,让用户专注于烹饪和享受美食。 健康菜谱App是健康饮食追求者的得力助手,无论是健身爱好者、素食主义者还是普通家庭,都能找到适合自己的健康食谱。立即下载健康菜谱App,开启健康饮食新篇章!
课设毕设基于SpringBoot+Vue的职称评审管理系统 LW+PPT+源码可运行.zip
MikelProjectDemo-dev
基于springboot的java毕业&课程设计
HUWEI eNSP课程作业
基于springboot的java毕业&课程设计