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的资源路径,返回文件的输入流
*@paramrelativePath必须传递资源的相对路径。
*@是相对于classpath的路径。
*@如果需要查找classpath外部的资源,需要使用../来查找
*@return 文件输入流
*@throw sIOException
*@throw sMalformedURLException
*/
public static InputStream getStream(String relativePath)
throws MalformedURLException, IOException
{
if (!relativePath.contains("../"))
{
return getClassLoader().getResourceAsStream(relativePath);
}
else
{
return ClassLoaderUtil.getStreamByExtendResource(relativePath);
}
}
/**
*
*@paramurl
*@return
*@throw sIOException
*/
public static InputStream getStream(URL url) throws IOException
{
if (url != null)
{
return url.openStream();
}
else
{
return null;
}
}
/**
*
*@paramrelativePath必须传递资源的相对路径。是相对于classpath的路径。
*@如果需要查找classpath外部的资源,需要使用../来查找
*@return
*@throw sMalformedURLException
*@throw sIOException
*/
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();
}
/**
*
*@paramrelativePath 必须传递资源的相对路径。是相对于classpath的路径。
*@如果需要查找classpath外部的资源,需要使用../来查找
*@return资源的绝对URL
*@throw sMalformedURLException
*/
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;
}
/**
*
*@paramsource
*@paramdest
*@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;
}
/**
*
*@paramsource
*@paramdest
*@paramnum
*@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);
}
return source;
}
/**
*
*@paramresource
*@return
*/
public static URL getResource(String resource)
{
ClassLoaderUtil.log.info("传入的相对于classpath的路径:" + resource);
return ClassLoaderUtil.getClassLoader().getResource(resource);
}
/**
*@paramargs
*@throw sMalformedURLException
*/
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());
}
}
分享到:
相关推荐
WPF中Image控件Source属性的相对路径和绝对路径问题的开发经验总结,亲测通过!
文中指出尽量不要使用相对于System.getProperty(\\\"user....使用上面 ClassLoaderUtil类的public static URL getExtendResource(String relativePath)方法已经能够使用相对于classpath的相对路径定位所有位置的资源。
主要介绍了springboot如何获取相对路径文件夹下静态资源的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
所以,如果在项目中需要使用相对路径来获取静态的资源文件,需要怎么做呢? 1、修改webpack配置文件中的assetsPublicPath,修改为如下图所示。 修改配置后,进行打包发现,打包后的index.html文件中,对css等文件的...
文件的路径通常有 相对路径 与 绝对路径。 2.1 相对路径 以当前文件为基准进行一级级目录指向被引用的资源文件。在 Java 代码中以当前运行的代码所在的位置为参照位置,只要被引用的文件相对于引用的文件的位置不变...
绝对URL用于表示Internet中特定文件所需要的全部内容,Internet中的每一个文件都...如果希望连接到相同目录中的资源,则只需要更换文件名字,而原始地址不需要更换。 1、子目录 电脑中的子目录很好理解,例如:C:\是
WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源。其中较为常见的情况是用Uri加载图像。Uri加载图像资源有多种方式,可从外部文件夹中加载,也可以直接引用工程的资源图片……
【SpringBoot页面跳转访问css.../resources :系统默认的根路径 /static :所有静态资源文件如js、css、jpg、html等文件是可以直接访问的 /templates :此目录下的文件是不可以直接访问的,需要经过控制器才可以跳转的
编程思路:首先获取请求的路径,路径为我们想要得到的资源,建立URL对象,抛出异常,得到HttpURLConnection对象,声明请求方式,设置连接超时,得到服务器传回来的数据,相对我们来说输入流,得到数据, 创建一个...
hm-tools-js-css 功能:根据URL请求参数将多个javascript文件合并成一个javascript文件返回...支持替换css文件中url引用资源的相对路径为可访问的绝对路径。支持使用yuicompressor对js,css文件内容进行实时压缩、混淆
最近在试水 Vue CLI 3,并且尝试配置一个多页面(多应用)项目出来,期间又遇到各种路径问题,于是…于是有了下面的唠叨。 以下都是基于 Vue CLI 3 来举例说明的,使用 2.x 版本的其实也类似 首先,参考 官方文档...
空调制冷维修改管理系统,Delphi7结合Access开发的,完成维修设备及资料的录入、维护管理等功能。程序中有一个网络函数库,觉得功能...同时程序还可换肤,要注意皮肤资源和程序相对路径不要搞错,要不然换肤时会出错。
页面中很多资源文件有时候我们都是用的相对路径引入的,但是遇到需要将地址传给第三方(如分享微博 微信时),往往需要传给完整的路径。此时简单暴力的写法是直接写个地址上去,该方法适用于已经预先知道将要上线的...
个人为完成学校特定科目所制作,压缩包内程序为自己设计编写,内部图片来源于校内分享的免费资源。本作品是实现红绿灯功能,且进行数字显示倒...自行设计了一个相对路径导入方法,省去每次打开文件都要手动导入的弊端。
本资源由大量的实用批处理文件组成,删除.txt尾缀名双击即可直接使用,既是学习的模板也可以作为实用程序,如下为文件组成,涉及文件管理,系统,网络,小工具等等...获取相对路径.cmd 获取路径中指定层深的字符串.cmd
找了好多个都是字节集资.源传来传去的,总感觉那样写...源包是直接通过打包目录生成的,通过相对路径获取资.源包里的资.源,所以没有什么索引取资.源,替换资源,删除资源什么的 所以主要用在UI资源包,模型资源包...
@Path,标注资源类或方法的相对路径 @GET,@PUT,@POST,@DELETE,标注方法是用的HTTP请求的类型 @Produces,标注返回的MIME媒体类型 @Consumes,标注可接受请求的MIME媒体类型 @PathParam,@QueryParam,@...
但两条链路之间如果不采用STP技术,可能形成二层环路,所有内网用户都需要从 WEB服务器上获取资源,所以WEB服务器必须挂接在核心交换机上。采用星型结构便于管 理,因为是小型局域网,不须再用会聚层交换机,节约...
@Path注解的值是一个相对的URI路径,这个路径指定了该Java类的位置,例如/helloworld。在这个URI中可以包含变量,例如可以获取用户的姓名然后作为参数传入URI中:/helloworld/{username}。 @GET注解是请求方法指示符...
<br>2007/7/04 Version 3.1.9 beta <br>Updates: 1) 增强页面信息采集功能的链接分析能力,当采集图片或超链接的时候会自动将相对路径转化为真实的网络路径,并且修正了采集功能的一些已知BUG。...