2012-01-10 18 views
1

我使用XML文件來保存我的SQL查詢。由於每個應用程序引擎的指導方針在這裏:http://code.google.com/appengine/kb/java.html#readfile在GAE中使用XML文件

我有一個文件在我的戰爭/ WEB-INF目錄下和負載是這樣的:

 DocumentBuilder db = dbf.newDocumentBuilder();   
     dom = db.parse("\\WEB-INF\\Queries.xml"); 

當我部署到GAE我看到這在日誌中:

access denied (java.io.FilePermission /\WEB-INF\Queries.xml read) 

我錯過了什麼嗎?

回答

2

它將路徑視爲絕對路徑,因此它說對該文件的訪問被拒絕。

改爲使用InputStream作爲參數的解析過載(InputStream is)。

試試這段代碼,它應該工作正常。

ServletContext context = getServletContext(); 
InputStream is = context.getRealPath("/WEB-INF/Queries.xml"); 
DocumentBuilder db = dbf.newDocumentBuilder();   
     dom = db.parse(is);// throws SAXException and IOException 
0

我發現沒有必要使用ServletContext來訪問外部文件。只需使用新文件(「WEB-INF/Queries.xml」)即可。 (注意缺乏斜線上的文件路徑的第一個字符)

例如,我使用它來訪問backends.xml文件:

File tBackendFile = new File("WEB-INF/backends.xml"); 
Document tBackendDoc = DocumentBuilderFactory.newInstance() 
              .newDocumentBuilder() 
              .parse(tBackendFile); 
+0

2十分感謝解決方案傢伙! 我用第二個,但接受第一個:P – 2012-01-11 08:07:08

+0

沒有probs,很高興幫助:p – 2012-01-11 08:12:18