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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

【Spring】Resource接口:ClassPathResource

發(fā)布時(shí)間:2024/9/19 javascript 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Spring】Resource接口:ClassPathResource 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

  • java 1.8
  • springboot 2.5.4

ClassPathResource讀取文件

ClassPathResource 表示從類路徑獲取資源,它使用線程上下文類加載器、給定的類加載器來(lái)加載資源。classpath 資源存在于類路徑中的文件系統(tǒng)中或 jar 包里。

ClassPathResource 常用構(gòu)造器

public ClassPathResource(String path); public ClassPathResource(String path, @Nullable ClassLoader classLoader); public ClassPathResource(String path, @Nullable Class<?> clazz);
  • public ClassPathResource(String path):使用默認(rèn)的類加載器記載 path 類路徑下的資源
  • public ClassPathResource(String path, @Nullable ClassLoader classLoader):使用指定的類加載器加載 path 類路徑下的資源
  • public ClassPathResource(String path, @Nullable Class clazz):只用指定的類加載 path 類路徑下的資源

示例1:讀取 classpath 中的文件(1.txt)

@SpringBootApplication public class Demo1Application {public static void main(String[] args) {SpringApplication.run(Demo1Application.class, args);}@Beanpublic CommandLineRunner checkImage() {return (args)->{ClassPathResource cpr = new ClassPathResource("1.txt");System.out.println("1.txt is exists : " + cpr.getFile().exists());};}}

項(xiàng)目目錄結(jié)構(gòu)

項(xiàng)目 ├─src │ ├─main │ │ ├─java │ │ │ └─com │ │ │ └─example │ │ │ └─demo │ │ │ └─Demo1Application.java │ │ └─resources │ │ ├─application.properties │ │ └─1.txt │ └─test └─pom.xml

執(zhí)行結(jié)果

. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.5.4)2021-08-31 23:02:31.892 INFO 109456 --- [ main] com.example.demo.Demo1Application : Starting Demo1Application using Java 1.8.0_144 on LAPTOP-F1O81IBU with PID 109456 (D:\sde\eclipse-workspace\demo-1\target\classes started by lhg in D:\sde\eclipse-workspace\demo-1) 2021-08-31 23:02:31.895 INFO 109456 --- [ main] com.example.demo.Demo1Application : No active profile set, falling back to default profiles: default 2021-08-31 23:02:32.265 INFO 109456 --- [ main] com.example.demo.Demo1Application : Started Demo1Application in 0.635 seconds (JVM running for 1.554) 1.txt is exists : true

示例2:找不到文件

代碼與示例1一樣。改變1.txt的位置(將文件移動(dòng)到項(xiàng)目根目錄)后,執(zhí)行結(jié)果為:

. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.5.4)2021-08-31 22:54:57.996 INFO 113616 --- [ main] com.example.demo.Demo1Application : Starting Demo1Application using Java 1.8.0_144 on LAPTOP-F1O81IBU with PID 113616 (D:\sde\eclipse-workspace\demo-1\target\classes started by lhg in D:\sde\eclipse-workspace\demo-1) 2021-08-31 22:54:57.997 INFO 113616 --- [ main] com.example.demo.Demo1Application : No active profile set, falling back to default profiles: default 2021-08-31 22:54:58.372 INFO 113616 --- [ main] com.example.demo.Demo1Application : Started Demo1Application in 0.616 seconds (JVM running for 1.517) 2021-08-31 22:54:58.375 INFO 113616 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-08-31 22:54:58.391 ERROR 113616 --- [ main] o.s.boot.SpringApplication : Application run failedjava.lang.IllegalStateException: Failed to execute CommandLineRunnerat org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:794) [spring-boot-2.5.4.jar:2.5.4]at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:775) [spring-boot-2.5.4.jar:2.5.4]at org.springframework.boot.SpringApplication.run(SpringApplication.java:345) [spring-boot-2.5.4.jar:2.5.4]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) [spring-boot-2.5.4.jar:2.5.4]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) [spring-boot-2.5.4.jar:2.5.4]at com.example.demo.Demo1Application.main(Demo1Application.java:13) [classes/:na] Caused by: java.io.FileNotFoundException: class path resource [1.txt] cannot be resolved to URL because it does not existat org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:202) ~[spring-core-5.3.9.jar:5.3.9]at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:150) ~[spring-core-5.3.9.jar:5.3.9]at com.example.demo.Demo1Application.lambda$0(Demo1Application.java:20) [classes/:na]at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791) [spring-boot-2.5.4.jar:2.5.4]... 5 common frames omitted

參考

https://blog.csdn.net/qq_16830879/article/details/91876712

總結(jié)

以上是生活随笔為你收集整理的【Spring】Resource接口:ClassPathResource的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。