我正在maven web應用程序(Java)中工作。我需要讀取一個目錄(例如:mydirectory中)在我的WebApp文件夾,如下所示,如何讀取maven應用程序的webapp文件夾中的目錄
File file = new File("path of my directory");
我指定我的應用程序文件夾的層次結構。
MyApplication->src->main->webapp->MyDirectory
但我會寫我的Java代碼我的包如下,
MyApplication->src->main->java->com->mypackage->myfile.java
從myfile.java我需要閱讀的webapp目錄 「mydirectory中」。作爲新的文件(「目錄的路徑」)
但我不知道如何在這裏指定目錄的路徑。
任何人都可以幫助我嗎?這很緊急。
由於提前, Gnik
我試過這個ServletContext context = getServletContext();它顯示錯誤找不到符號getServletContext()。幫我 – Prince 2012-02-18 04:46:46
如果你不在一個繼承HttpServlet的類中,你需要有一個你可以從中獲得會話的請求。從會話對象中,您可以調用getServletContext方法。例如request.getSession()。getServletContext()方法 – 2012-02-18 10:19:51