简体   繁体   中英

Load JSON File on the server side in GWT

I have a GWT application and I load a .json file on the server side like this:

InputStream source = new FileInputStream(testFile.json); 

This works perfectly when I start the application directly in eclipse. However, when I deploy the app on tomcat, it does not work. It seems like, that the application is looking for that file in the bin folder of tomcat (???). However, the correct path would be tomcat/webapps/myProject/testFile.json.

Does anyone know how to get the correct path (without harcoding it)?

The FileInputStream locates files depending on the current working directory, which in turn depends on the way how you started the application, which in turn is thus not controllable from inside your application. In case of web applications, you need ServletContext#getResourceAsStream() instead of FileInputStream to obtain web application's own resources. It takes a path which is relative to the web content folder.

InputStream input = getServletContext().getResourceAsStream("/testfile.json");
// ...

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM