簡體   English   中英

創建bean時找不到Spring MVC文件異常

[英]Spring MVC file not found exception when creating bean

我有以下目錄結構:

在此處輸入圖片說明

我通過以下方式創建一個bean:

@Configuration
public class Create {
public static String path = new File("src/main/webapp/resources/nodes.txt").getAbsolutePath();
@Bean
public static Graph returnGraph() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
[rest of the code]
    return graph;
}
}

我想閱讀位於DeployedResources / webapp / resources中的文件nodes.txt

但是我得到了一個java.io.FileNotFoundException。 現在,如果我指定文件的絕對路徑(如果我的文件位於桌面中),這將起作用。 但我無法執行此操作,否則它將無法在另一台計算機上運行。

我試圖指定一個相對路徑,因此它將在所有機器上都可以使用,但是當我將其作為Spring MVC應用程序運行時,工作目錄就會更改。 我該怎么辦才能從任何計算機讀取文件?

嘗試替換此路徑:

 new File("src/main/webapp/resources/nodes.txt").getAbsolutePath();

隨着刪除getAbsolutePath()

  new File("src/main/webapp/resources/nodes.txt");      

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM