2017-02-16 18 views
0

我試圖訪問GeoLiteCity文件,它在我的本地機器上工作,但無法獲取雲(Azure)上的路徑。 文件保存恰好一樣層次:訪問雲環境中的本地文件

/src/main/resources/database/GeoLiteCity.dat 

我的代碼:

void getFile() { 
    if (this.file == null) { 
     try { 
      URL url = this.getClass().getClassLoader().getResource("database/GeoLiteCity.dat"); 
      this.file = new File(url.toURI()); 
      System.out.println(file.getPath().toString()); 

     } catch (Exception ex) { 
      System.out.println(file.getPath().toString(),ex); 
     } 
    } 
} 
+0

您使用了什麼Azure服務? Azure WebApps或雲服務? –

回答

0

假設代碼是在Tomcat上運行的Java Web應用程序的一部分。根據我的經驗,如果您想通過方法this.getClass().getClassLoader().getResource("<the data file path relative to the root classes path>")獲取資源,請確保數據文件GeoLiteCity.dat在路徑webapps/<your-webapp-name>/WEB-INF/classes/database/下。

如果您的webapp部署在Azure WebApp上,我認爲您可以嘗試使用Azure WebApp上的數據文件的絕對路徑,例如D:/home/site/wwwroot/webapps/<your-webapp-name>/<the data file path>,並且只能使用new File("<the absolute path of data file>")