[英]Spring Boot access static resources missing scr/main/resources
我正在開發一個 Spring Boot 應用程序。 我需要在開始時解析一個 XML 文件 (countries.xml)。 問題是我不知道把它放在哪里以便我可以訪問它。 我的文件夾結構是
ProjectDirectory/src/main/java
ProjectDirectory/src/main/resources/countries.xml
我的第一個想法是將它放在 src/main/resources 中,但是當我嘗試創建文件 (countries.xml) 時,我得到了一個 NPE 並且堆棧跟蹤顯示我的文件在 ProjectDirectory 中(因此 src/main/resources/沒有添加)。 我嘗試創建文件 (resources/countries.xml) 並且路徑看起來像 ProjectDirectory/resources/countries.xml(所以再次沒有添加 src/main)。
我嘗試添加這個但沒有結果
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addResourceHandlers(registry);
}
我知道我可以手動添加 src/main/,但我想了解為什么它不能正常工作。 我還嘗試了 ResourceLoader 的示例 - 沒有結果。
任何人都可以提出問題是什么?
更新:僅供將來參考 - 構建項目后,我遇到了訪問文件的問題,因此我將 File 更改為 InputStream
InputStream is = new ClassPathResource("countries.xml").getInputStream();
只需使用 Spring 類型ClassPathResource 。
File file = new ClassPathResource("countries.xml").getFile();
只要這個文件在類路徑上的某個地方,Spring 就會找到它。 這可以是開發和測試期間的src/main/resources
。 在生產中,它可以是當前運行目錄。
編輯:如果文件在 fat JAR 中,則此方法不起作用。 在這種情況下,您需要使用:
InputStream is = new ClassPathResource("countries.xml").getInputStream();
要獲取類路徑中的文件:
Resource resource = new ClassPathResource("countries.xml");
File file = resource.getFile();
要讀取文件 onStartup 使用@PostConstruct
:
@Configuration
public class ReadFileOnStartUp {
@PostConstruct
public void afterPropertiesSet() throws Exception {
//Gets the XML file under src/main/resources folder
Resource resource = new ClassPathResource("countries.xml");
File file = resource.getFile();
//Logic to read File.
}
}
這是一個在 Spring Boot App 啟動時讀取 XML 文件的小示例。
在使用 Spring Boot 應用程序時,當它部署為 JAR 時,很難使用resource.getFile()
獲取類路徑資源,因為我遇到了同樣的問題。 使用 Stream 解決此掃描,它將找出放置在類路徑中任何位置的所有資源。
以下是相同的代碼片段 -
ClassPathResource classPathResource = new ClassPathResource("fileName");
InputStream inputStream = classPathResource.getInputStream();
content = IOUtils.toString(inputStream);
我使用彈簧靴,所以我可以簡單地使用:
File file = ResourceUtils.getFile("classpath:myfile.xml");
您需要使用以下構造
InputStream in = getClass().getResourceAsStream("/yourFile");
請注意,您必須在文件名之前添加此斜杠。
您可以使用以下代碼從資源文件夾中讀取字符串中的文件。
final Resource resource = new ClassPathResource("public.key");
String publicKey = null;
try {
publicKey = new String(Files.readAllBytes(resource.getFile().toPath()), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
我使用 Spring Boot,我對問題的解決方案是
"src/main/resources/myfile.extension"
希望它可以幫助某人。
因為 java.net.URL 不足以處理各種底層資源,Spring 引入了 org.springframework.core.io.Resource。 要訪問資源,我們可以使用 @Value 注解或 ResourceLoader 類。 @Autowired 私有 ResourceLoader 資源加載器;
@Override public void run(String... args) 拋出異常 {
Resource res = resourceLoader.getResource("classpath:thermopylae.txt");
Map<String, Integer> words = countWords.getWordsCount(res);
for (String key : words.keySet()) {
System.out.println(key + ": " + words.get(key));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.