日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
在SpringBoot中從類路徑加載文件

在下面,您將找到在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()該方法,則會收到錯誤消息,因為Spring嘗試訪問文件系統(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.     } 

網(wǎng)頁名稱:在SpringBoot中從類路徑加載文件
本文路徑:http://www.5511xx.com/article/ccohogd.html