在 GWT 中的服务器端加载 JSON 文件
我有一个 GWT 应用程序,并在服务器端加载一个 .json 文件,如下所示:
InputStream source = new FileInputStream(testFile.json);
当我直接在 Eclipse 中启动应用程序时,这非常有效。但是,当我将应用程序部署到tomcat上时,它不起作用。看起来,应用程序正在 tomcat 的 bin 文件夹中查找该文件(???)。但是,正确的路径是 tomcat/webapps/myProject/testFile.json。
有谁知道如何获得正确的路径(无需对其进行编码)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FileInputStream 根据当前工作目录定位文件,而当前工作目录又取决于您启动应用程序的方式,因此无法从应用程序内部控制。对于 Web 应用程序,您需要
ServletContext#getResourceAsStream()
而不是FileInputStream
来获取 Web 应用程序自己的资源。它采用相对于 Web 内容文件夹的路径。另请参阅:
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 needServletContext#getResourceAsStream()
instead ofFileInputStream
to obtain web application's own resources. It takes a path which is relative to the web content folder.See also: