[英]How to read XML file? Google App Engine Endpoints
我有:
wordsDictionary.xml
在:
/WEB-INF/xml/
我正在嘗試使用SAXReader
使用代碼閱讀它:
...
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setValidating(false);
saxFactory.setNamespaceAware(false);
XMLReader reader = saxFactory.getXMLReader();
reader.setContentHandler(new WordsDictionarySAXHandler(this,lettersMapping));
reader.parse(new InputSource("/WEB-INF/xml/wordsDictionary.xml"));
...
在Dev服務器上一切都很好。 將其部署到生產時,我收到一個錯誤:
com.google.api.server.spi.SystemService invokeServiceMethod: access denied
("java.io.FilePermission" "/WEB-INF/xml/wordsDictionary.xml" "read")
java.security.AccessControlException: access denied ("java.io.FilePermission"
"/WEBINF/xml/wordsDictionary.xml" "read")
在您說我需要使用: context.getResourceAsStream
您需要知道我正在使用Google端點 。 所以我沒有訪問任何直接的Servlet (沒有任何東西傳遞給端點方法 - HttpRequest
)。
我的問題是:你知道如何在GAE端點上加載xml文件嗎?
編輯:請閱讀https://developers.google.com/appengine/kb/java?csw=1#readfile :
如果文件位置不是問題,則問題可能是您用於從文件中讀取的方法未列入白名單。 您的應用程序可以使用任何對從文件系統讀取有用的IO類,例如File,FileInputStream,FileReader或RandomAccessFile。 有關列入白名單的完整列表,請參閱JRE班級白名單。
我已將部分代碼更改為:
FileInputStream stream = new FileInputStream("/WEB-INF/xml/wordsDictionary.xml");
reader.parse(new InputSource(new InputStreamReader(stream)));
不幸的是,沒有改變。
錯誤原因是WEB-INF之前的“/”。 記得:
access denied ("java.io.FilePermission"
"/WEBINF/xml/wordsDictionary.xml" "read")
即使您嘗試訪問錯誤的目錄,也可能拋出此錯誤。
還要記住為XML文件設置正確的編碼:
FileInputStream stream = new FileInputStream("WEB-INF/xml/wordsDictionary.xml");
InputSource is = new InputSource(new InputStreamReader(stream, "UTF-8"));
is.setEncoding("UTF-8");
reader.parse(is);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.