專案中很多時候需要讀取自訂設定文件,本地開發工具怎麼寫都成功但是部署到服務其上就出現問題,
異常BOOT-INF/classes !/config.xml (文件名、目錄名或卷標語法不正確.)路徑中帶有嘆號之類的
了解了大概之後就是springboot打成jar是一個文件,也就是一個壓縮包,沒有辦法讀取壓縮檔裡的路徑,因此要解決這個問題了解讀取設定檔的原理,直接取得檔案流就可以了。
1、使用專案內路徑讀取,只能在開發工具中使用,部署之後無法讀取。 (不通用)
類似:src/main/resources/default.xml
File file = new File("src/main/resources/default.xml" );
@Test public void testReadFile2() throws IOException { File file = new File("src/main/resources/default.xml"); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); fis.close(); }
2、使用org.springframework.util.ResourceUtils,讀取。在linux環境中無法讀取。 (不通用)
#File file = ResourceUtils.getFile("classpath:default.xml");
FileInputStream fis = new FileInputStream(file);
@Test public void testReadFile3() throws IOException { File file = ResourceUtils.getFile("classpath:default.xml"); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); fis.close(); }
3、使用org.springframework.core.io.ClassPathResource,各種環境都能讀取。 (通用)
Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();
@Test public void testReadFile() throws IOException { // ClassPathResource classPathResource = new ClassPathResource("default.xml"); Resource resource = new ClassPathResource("default.xml"); InputStream is = resource.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); is.close(); }
#4、結合spring註解,使用org.springframework.core.io.ResourceLoader;類別的註解。 (通用)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Autowired ResourceLoader resourceLoader; @Test public void testReaderFile() throws IOException { Resource resource = resourceLoader.getResource("classpath:default.xml"); InputStream is = resource.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); is.close(); } }
以上是springboot如何讀取resources下的文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!