日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

获取资源文件工具类

發布時間:2025/3/8 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取资源文件工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果沒有依賴spring,可以將分割線下的方法去掉

import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.util.ResourceUtils;import java.io.*; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; import java.util.Properties;public class Resources {private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();private static Charset charset;Resources() {}public static ClassLoader getDefaultClassLoader() {return classLoaderWrapper.defaultClassLoader;}public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {classLoaderWrapper.defaultClassLoader = defaultClassLoader;}public static URL getResourceURL(String resource) throws IOException {return getResourceURL((ClassLoader)null, resource);}public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {URL url = classLoaderWrapper.getResourceAsURL(resource, loader);if(url == null) {throw new IOException("Could not find resource " + resource);} else {return url;}}public static InputStream getResourceAsStream(String resource) throws IOException {return getResourceAsStream((ClassLoader)null, resource);}public static InputStream getResourceAsStream(Class<?> clazz, String resource) throws IOException {InputStream in = classLoaderWrapper.getResourceAsStream(resource, clazz);if(in == null) {throw new IOException("Could not find resource " + resource);} else {return in;}}public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);if(in == null) {throw new IOException("Could not find resource " + resource);} else {return in;}}public static Properties getResourceAsProperties(String resource) throws IOException {Properties props = new Properties();InputStream in = getResourceAsStream(resource);props.load(in);in.close();return props;}public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {Properties props = new Properties();InputStream in = getResourceAsStream(loader, resource);props.load(in);in.close();return props;}public static Reader getResourceAsReader(String resource) throws IOException {InputStreamReader reader;if(charset == null) {reader = new InputStreamReader(getResourceAsStream(resource));} else {reader = new InputStreamReader(getResourceAsStream(resource), charset);}return reader;}public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {InputStreamReader reader;if(charset == null) {reader = new InputStreamReader(getResourceAsStream(loader, resource));} else {reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);}return reader;}public static File getResourceAsFile(String resource) throws IOException {return new File(getResourceURL(resource).getFile());}public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {return new File(getResourceURL(loader, resource).getFile());}public static InputStream getUrlAsStream(String urlString) throws IOException {URL url = new URL(urlString);URLConnection conn = url.openConnection();return conn.getInputStream();}public static Reader getUrlAsReader(String urlString) throws IOException {InputStreamReader reader;if(charset == null) {reader = new InputStreamReader(getUrlAsStream(urlString));} else {reader = new InputStreamReader(getUrlAsStream(urlString), charset);}return reader;}public static Properties getUrlAsProperties(String urlString) throws IOException {Properties props = new Properties();InputStream in = getUrlAsStream(urlString);props.load(in);in.close();return props;}public static Class<?> classForName(String className) throws ClassNotFoundException {return classLoaderWrapper.classForName(className);}public static Charset getCharset() {return charset;}public static void setCharset(Charset charset) {charset = charset;}//############################ 華麗分割線 通過 spring 工具類 #################################public static File getFileWithResourceUtils(String resource) throws FileNotFoundException {return ResourceUtils.getFile(resource);}public Resource getResourceWithPathMatchingResourcePatternResolver(String resource) {PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();return pathMatchingResourcePatternResolver.getResource(resource);}public Resource[] getResourcesWithPathMatchingResourcePatternResolver(String resource) throws IOException {PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();return pathMatchingResourcePatternResolver.getResources(resource);}public InputStream getInputStreamWithClassPathResource(String resource, Class<?> clazz) throws IOException {if (clazz != null) {return new ClassPathResource(resource, clazz).getInputStream();} else {return new ClassPathResource(resource).getInputStream();}} }class ClassLoaderWrapper {ClassLoader defaultClassLoader;ClassLoader systemClassLoader;ClassLoaderWrapper() {try {this.systemClassLoader = ClassLoader.getSystemClassLoader();} catch (SecurityException var2) {;}}public URL getResourceAsURL(String resource) {return this.getResourceAsURL(resource, this.getClassLoaders((ClassLoader)null));}public URL getResourceAsURL(String resource, ClassLoader classLoader) {return this.getResourceAsURL(resource, this.getClassLoaders(classLoader));}public InputStream getResourceAsStream(String resource) {return this.getResourceAsStream(resource, this.getClassLoaders((ClassLoader)null));}public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {return this.getResourceAsStream(resource, this.getClassLoaders(classLoader));}public Class<?> classForName(String name) throws ClassNotFoundException {return this.classForName(name, this.getClassLoaders((ClassLoader)null));}public Class<?> classForName(String name, ClassLoader classLoader) throws ClassNotFoundException {return this.classForName(name, this.getClassLoaders(classLoader));}InputStream getResourceAsStream(String resource, ClassLoader[] classLoader) {ClassLoader[] arr$ = classLoader;int len$ = classLoader.length;for(int i$ = 0; i$ < len$; ++i$) {ClassLoader cl = arr$[i$];if(null != cl) {InputStream returnValue = cl.getResourceAsStream(resource);if(null == returnValue) {returnValue = cl.getResourceAsStream("/" + resource);}if(null != returnValue) {return returnValue;}}}return null;}URL getResourceAsURL(String resource, ClassLoader[] classLoader) {ClassLoader[] arr$ = classLoader;int len$ = classLoader.length;for(int i$ = 0; i$ < len$; ++i$) {ClassLoader cl = arr$[i$];if(null != cl) {URL url = cl.getResource(resource);if(null == url) {url = cl.getResource("/" + resource);}if(null != url) {return url;}}}return null;}Class<?> classForName(String name, ClassLoader[] classLoader) throws ClassNotFoundException {ClassLoader[] arr$ = classLoader;int len$ = classLoader.length;for(int i$ = 0; i$ < len$; ++i$) {ClassLoader cl = arr$[i$];if(null != cl) {try {Class<?> c = Class.forName(name, true, cl);if(null != c) {return c;}} catch (ClassNotFoundException var8) {;}}}throw new ClassNotFoundException("Cannot find class: " + name);}ClassLoader[] getClassLoaders(ClassLoader classLoader) {return new ClassLoader[]{classLoader, this.defaultClassLoader, Thread.currentThread().getContextClassLoader(), this.getClass().getClassLoader(), this.systemClassLoader};}public InputStream getResourceAsStream(String resource, Class<?> clazz) {return clazz.getResourceAsStream(resource);} }

測試方法

try {File file = ResourceUtils.getFile("classpath:" + Resources.class.getName().replace(".", "/") + ".class");BufferedReader br = new BufferedReader(new FileReader(file));String str = null;while ((str = br.readLine()) != null) {System.out.println(str);} } catch (FileNotFoundException e) {e.printStackTrace(); } catch (IOException e) {e.printStackTrace(); }try {PathMatchingResourcePatternResolver p = new PathMatchingResourcePatternResolver();BufferedReader br = new BufferedReader(new InputStreamReader(p.getResource(Resources.class.getName().replace(".", "/") + ".class").getInputStream()));String str = null;while ((str = br.readLine()) != null) {System.out.println(str);} } catch (IOException e) {e.printStackTrace(); }

//classpath 加不加都可以呦 try { PathMatchingResourcePatternResolver p = new PathMatchingResourcePatternResolver(); BufferedReader br = new BufferedReader(new InputStreamReader(p.getResources("classpath:"+Resources.class.getName().replace(".", "/") + ".class")[0].getInputStream())); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } try { BufferedReader br = new BufferedReader(new InputStreamReader(Resources.getResourceAsStream(Resources.class.getName().replace(".", "/") + ".class"))); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } try { BufferedReader br = new BufferedReader(new InputStreamReader(Resources.getResourceAsStream(Resources.class,"Resources.class"))); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } try { BufferedReader br = new BufferedReader(new InputStreamReader(new ClassPathResource(Resources.class.getName().replace(".", "/") + ".class").getInputStream())); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } try { BufferedReader br = new BufferedReader(new InputStreamReader(new ClassPathResource("Resources.class", Resources.class).getInputStream())); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } try { BufferedReader br = new BufferedReader(new InputStreamReader(Resources.getResourceAsStream(Resources.class.getName().replace(".", "/") + ".class"))); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } } catch (IOException e) { e.printStackTrace(); }

?

可以獲取到多個,包括我們自己定義的Resources.class

try {PathMatchingResourcePatternResolver p = new PathMatchingResourcePatternResolver();BufferedReader br = new BufferedReader(new InputStreamReader(p.getResources("classpath*:com/**/Resources.class")[0].getInputStream()));String str = null;while ((str = br.readLine()) != null) {System.out.println(str);} } catch (IOException e) {e.printStackTrace(); }

不可以獲取到

try {PathMatchingResourcePatternResolver p = new PathMatchingResourcePatternResolver();BufferedReader br = new BufferedReader(new InputStreamReader(p.getResources("classpath:com/**/Resources.class")[0].getInputStream()));String str = null;while ((str = br.readLine()) != null) {System.out.println(str);} } catch (IOException e) {e.printStackTrace(); }

原因看一下 方法的源代碼就發現了哦!

public Resource[] getResources(String locationPattern) throws IOException {Assert.notNull(locationPattern, "Location pattern must not be null");if(locationPattern.startsWith("classpath*:")) {return this.getPathMatcher().isPattern(locationPattern.substring("classpath*:".length()))?this.findPathMatchingResources(locationPattern):this.findAllClassPathResources(locationPattern.substring("classpath*:".length()));} else {int prefixEnd = locationPattern.indexOf(":") + 1;return this.getPathMatcher().isPattern(locationPattern.substring(prefixEnd))?this.findPathMatchingResources(locationPattern):new Resource[]{this.getResourceLoader().getResource(locationPattern)};} }

  findPathMatchingResources方法中調用getResources 最后執行的 代碼中標紅色的部分,通過resourceLoader返回資源。

  classpath*: 和 classpath: 不同的地方看一下getResources方法中 代碼中標紅色的 部分。?

轉載于:https://www.cnblogs.com/hujunzheng/p/7219303.html

總結

以上是生活随笔為你收集整理的获取资源文件工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 婷婷久久五月天 | 波多野结衣一二三区 | 免费av福利 | 依人综合网 | 欧美丰满熟妇bbb久久久 | 四虎网站 | 国产精品日日夜夜 | 国产精品mm | 国产一级片免费在线观看 | 欧美videossex极品 | 裸体男女树林做爰 | 免费视频国产 | 欧美老女人视频 | 欧美日韩久 | 国产中文网 | 免费又黄又爽又猛大片午夜 | 色999日韩| 狠狠入| 在线看成人av | 日韩精品专区 | 欧美精品在欧美一区二区 | 亚洲视频综合网 | 五月天天 | 免费成人深夜夜 | 国产精品二区在线观看 | 啦啦啦视频在线观看 | 色乱码一区二区三区网站 | 免费观看日韩毛片 | 亚洲黄在线观看 | 日本黄视频在线观看 | 日韩一区av在线 | 91精品婷婷国产综合久久 | 亚洲欧美中文日韩在线观看 | 丰满少妇被猛烈进入无码 | 国产二区自拍 | 日韩精品人妻无码一本 | 久久五月天综合 | 狠狠操夜夜 | 97在线超碰 | 精品欧美一区二区三区成人 | 日本阿v视频 | 亚洲一区二区三区不卡视频 | 九一亚洲精品 | 精品在线免费播放 | 97超碰人人网 | 欧美一区二区三区公司 | 黄网站在线免费 | 暖暖成人免费视频 | 精品成人18 | 精品1区2区3区 | 久久久久久久亚洲av无码 | 伊朗做爰xxxⅹ性视频 | 影音先锋二区 | 爱爱爱爱网站 | 亚洲天堂男人的天堂 | 捆绑无遮挡打光屁股 | 日本免费一区二区三区四区五六区 | 亚洲精品一区二区二区 | 免费成人进口网站 | 女人脱裤子让男人捅 | 欧美激情自拍偷拍 | 91在线免费视频观看 | 久艹在线观看视频 | 色999五月色 | 欧美一级黄色网 | 日本伦理片在线看 | 光棍天堂av| www.成人av| 小妹色播 | 日韩一区二区欧美 | 色干综合 | 91在线观看.| 国产精品视频大全 | 亚洲宅男天堂 | 亚洲色图 美腿丝袜 | 91九色偷拍 | 日韩欧美一级在线 | 天堂中文视频在线 | 免费观看色 | 久久精品黄 | 海量av| 激情综合婷婷 | 日日日视频| 欧美视频一级 | 成人免费观看cn | 国产美女精品久久久 | 变态另类丨国产精品 | 囯产精品久久久久久 | 你懂的91| 免费性爱视频 | 玖草在线观看 | 激情av在线 | 人人精品久久 | 亚洲激情文学 | 久艹在线播放 | 久久福利精品 | 色妹子综合 | 97超碰超碰| 牛av在线|