从App Engine模块中读取文件 [英] Reading a file from within App Engine module

查看:121
本文介绍了从App Engine模块中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从位于WEB-INF目录内的App Engine模块中读取静态文件。

  ... 
档案档案=新档案(WEB-INF / somestaticfile.ext);
...

它在云环境中可以很好地工作,但是在开发服务器中, code> FileNotFoundException 。



我使用maven构建和运行应用程序,似乎还需要预先一个代表项目EAR层次结构中模块的目录,以便找到该文件。

  ... 
文件file = new File(modulename.war / WEB-INF / somestaticfile.ext);
...

这适用于本地开发服务器,但不适用于云环境。 p>

我相信有一种方法可以读取可以在云环境和本地环境中工作的文件。任何意见,将不胜感激。

解决方案

我花了一些时间弄清楚这里是我找到的解决方案。如果您将资源放置在classpath上,那么您可以按照以下方式从云和本地开发服务器访问它们。

  getClass() 。.getClassLoader()的getResource( 资源名称); 

 的getClass()getClassLoader()的getResourceAsStream( 资源名称)。。 

但是,如果您的资源位于其他位置(例如WEB-INF)通过 javax.servlet.ServletContext#getResource(String)> javax.servlet.ServletContext#getResourceAsStream(String)

由于我使用Spring,我决定通过 org.springframework.core.io.ResourceLoader#getResource(String)返回资源抽象,其具体实现取决于应用程序上下文类型和资源路径前缀。对于运行在Web容器中的应用程序,实际的实现使用 ServletContext 方法,但是你屏蔽了它。



<如果你不使用Spring,根据 ServletContext 实现你自己的资源抽象应该不是什么大问题。



如果任何人可以提供其他解决方案,请随时这样做。

I am trying to read a static file from within App Engine module that is placed inside WEB-INF directory.

...
File file = new File("WEB-INF/somestaticfile.ext);
...

It perfectly works in cloud environment, however, in development server it ends up with FileNotFoundException.

I build and run the app with the aid of maven and it seems that it is also needed to prepend a directory representing the module inside the project's EAR hierarchy so that the file would be found.

...
File file = new File("modulename.war/WEB-INF/somestaticfile.ext);
...

This works in local development server but not in cloud environment.

I believe that there is a way how to read a file that would work in both cloud and local environment. Any advice would be appreciated.

解决方案

I have spent some time to figure it out and here are solutions I have found. If you have your resources placed on classpath then you can access them as follows from both cloud and local dev server.

getClass().getClassLoader().getResource("resourceName");

or

getClass().getClassLoader().getResourceAsStream("resourceName");

However, if your resources are place somewhere else (e.g. WEB-INF), the only way I have found is by means of javax.servlet.ServletContext#getResource(String) or javax.servlet.ServletContext#getResourceAsStream(String) that also work in both environments. This solution is perfectly OK if you access resources from web layer of your app but if you need to access them from other layers (e.g. service layer), it is not probably a good idea to introduce a dependency to Java Servlet API to that layer.

Since I am using Spring, I decided to load resources via org.springframework.core.io.ResourceLoader#getResource(String) that returns a resource abstraction whose concrete implementation depends on the application context type and a resource path prefix. For applications running in a web container, the actual implementation uses ServletContext methods under the hood but you are shielded from it.

If you do not use Spring, it should not be a big deal to implement your own resource abstraction based on ServletContext.

If anyone could provide other solutions, feel free to do so.

这篇关于从App Engine模块中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆