成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

在Spring Boot中從類路徑加載文件

開發(fā)
創(chuàng)建Spring Boot Web應(yīng)用程序時(shí),有時(shí)有時(shí)需要從類路徑中加載文件;war和jar的加載文件格式是不一樣的

[[344641]]

在下面,您將找到在WAR和JAR中加載文件的解決方案。

資源加載器
使用Java,您可以使用當(dāng)前線程的classLoader并嘗試加載文件,但是Spring Framework為您提供了更為優(yōu)雅的解決方案,例如ResourceLoader。

您只需要自動連接ResourceLoader,然后調(diào)用getResource(„somePath“)方法即可。

在Spring Boot(WAR)中從資源目錄/類路徑加載文件的示例
在以下示例中,我們從類路徑中加載名為GeoLite2-Country.mmdb的文件作為資源,然后將其作為File對象檢索。

  1. @Service("geolocationservice"
  2. public class GeoLocationServiceImpl implements GeoLocationService { 
  3.     private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class); 
  4.     private static DatabaseReader reader = null
  5.     private ResourceLoader resourceLoader; 
  6.     @Autowired 
  7.     public GeoLocationServiceImpl(ResourceLoader resourceLoader) { 
  8.         this.resourceLoader = resourceLoader; 
  9.     }    @PostConstruct 
  10.     public void init() { 
  11.         try { 
  12.             LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database..."); 
  13.             Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb"); 
  14.             File dbAsFile = resource.getFile();            // Initialize the reader 
  15.             reader = new DatabaseReader 
  16.                         .Builder(dbAsFile) 
  17.                         .fileMode(Reader.FileMode.MEMORY) 
  18.                         .build(); 
  19.             LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully."); 
  20.         } catch (IOException | NullPointerException e) { 
  21.             LOGGER.error("Database reader cound not be initialized. ", e); 
  22.         } 
  23.     } 
  24.     @PreDestroy 
  25.     public void preDestroy() { 
  26.         if (reader != null) { 
  27.             try { 
  28.                 reader.close(); 
  29.             } catch (IOException e) { 
  30.                 LOGGER.error("Failed to close the reader."); 
  31.             } 
  32.         } 
  33.     } 

在Spring Boot(JAR)中從資源目錄/類路徑加載文件的示例
如果您想從Spring Boot JAR中的 classpath加載文件,則必須使用該resource.getInputStream()方法將其作為InputStream檢索。如果嘗試使用resource.getFile()該方法,則會收到錯(cuò)誤消息,因?yàn)镾pring嘗試訪問文件系統(tǒng)路徑,但無法訪問JAR中的路徑。

  1. @Service("geolocationservice"
  2. public class GeoLocationServiceImpl implements GeoLocationService { 
  3.     private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class); 
  4.     private static DatabaseReader reader = null
  5.     private ResourceLoader resourceLoader; 
  6.     @Inject 
  7.     public GeoLocationServiceImpl(ResourceLoader resourceLoader) { 
  8.         this.resourceLoader = resourceLoader; 
  9.     }    @PostConstruct 
  10.     public void init() { 
  11.         try { 
  12.             LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database..."); 
  13.             Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb"); 
  14.             InputStream dbAsStream = resource.getInputStream(); // <-- this is the difference 
  15.             // Initialize the reader 
  16.             reader = new DatabaseReader 
  17.                         .Builder(dbAsStream) 
  18.                         .fileMode(Reader.FileMode.MEMORY) 
  19.                         .build(); 
  20.             LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully."); 
  21.         } catch (IOException | NullPointerException e) { 
  22.             LOGGER.error("Database reader cound not be initialized. ", e); 
  23.         } 
  24.     } 
  25.     @PreDestroy 
  26.     public void preDestroy() { 
  27.         if (reader != null) { 
  28.             try { 
  29.                 reader.close(); 
  30.             } catch (IOException e) { 
  31.                 LOGGER.error("Failed to close the reader."); 
  32.             } 
  33.         } 
  34.     } 

 

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2024-10-08 09:06:01

Spring應(yīng)用程序配置

2024-04-23 14:13:38

開發(fā)配置文件

2023-09-22 10:12:57

2024-09-30 11:51:07

2019-12-09 15:08:30

JavaTomcatWeb

2024-01-23 08:47:13

BeanSpring加載方式

2022-09-02 08:19:37

spring配置加載

2011-07-07 17:30:25

iPhone Xcode

2024-06-24 14:52:50

Android類加載器

2025-06-26 03:33:00

2019-07-15 16:00:24

Docker架構(gòu)容器

2024-09-05 09:35:58

CGLIBSpring動態(tài)代理

2024-10-10 14:43:54

LambdaSpring編程

2019-07-15 10:00:53

DockerJava容器

2021-09-01 10:07:43

開發(fā)零搭建Groovy

2022-11-01 08:55:55

編譯工具MavenSpring

2021-10-18 10:36:31

Spring Boot插件Jar

2021-10-18 12:04:22

Spring BootJava開發(fā)

2024-09-11 08:22:44

2024-08-09 11:50:00

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 日本成人三级电影 | 亚洲美乳中文字幕 | 亚洲国产精品第一区二区 | 在线观看特色大片免费网站 | 久久性| 中文在线一区 | 在线观看日韩 | 亚洲欧美网站 | 国产精品地址 | 久久久久久艹 | 久久久蜜桃一区二区人 | 欧美日韩在线一区二区 | 久久久精品网 | 性高湖久久久久久久久3小时 | 一区影院| 亚洲精品18 | 国产精品美女在线观看 | av永久| 国产日韩欧美在线一区 | 黄色在线观看国产 | 国产精品国产三级国产播12软件 | 日韩福利| 亚洲日本乱码在线观看 | 91就要激情 | 国产精品九九九 | 国产精品一区二区三区四区 | 日韩一区二区福利视频 | 国产精品久久久久久久久久久久久 | 国产97久久| 欧美日韩久久久久 | 欧美精品久久久 | 国产欧美精品一区二区三区 | av在线影院 | 欧美在线观看一区 | 欧美亚洲国产成人 | 欧美国产精品一区二区 | 国产一区二区 | 一区二区三区国产精品 | 午夜男人的天堂 | 日韩精品在线免费观看 | 欧美日高清 |