简体   繁体   English

在Openshift上的servlet中打开资源文件

[英]Opening a resource file in a servlet on Openshift

I'm in troubles with opening file within my web-app. 我在网络应用程序中打开文件时遇到了麻烦。 I tried it locally within Eclipse and it works fine but when I try to deploy it on Tomcat 6 on Openshift it doesn't find resource files for my web-app. 我在Eclipse中本地尝试它并且它工作正常但是当我尝试在Openshift上的Tomcat 6上部署它时,它找不到我的web-app的资源文件。 There are some txt files in a ProjectFiles directory stored in WEB-INF directory; ProjectFiles目录中有一些txt文件存储在WEB-INF目录中; the code that locally opens file is 本地打开文件的代码是

String nome_file = "C\:\\Users\\miKKo\\workspace\\fantacalcio_project\\WebContent\\WEB-INF\\ProjectFiles\\Risultati\\risultati_" + nome_lega + ".txt"; 

BufferedReader reader = new BufferedReader(new FileReader(nome_file));

I've pushed them within Git in the same repository (on server I renamed my project in "ROOT") and I've substituted string with this 我已经在同一个存储库中将它们推送到Git中(在服务器上我在“ROOT”中重命名了我的项目)并且我用这个替换了字符串

String nome_file = this.getServletConfig().getServletContext().getContextPath()+"/WebContent/WEB-INF/ProjectFiles/Risultati/risultati_" + nome_lega + ".txt";

but it doesn't work. 但它不起作用。 I've also tried with a context attribute 我也尝试过上下文属性

/var/lib/openshift/51c6178a5004467630000019/jbossews/work/Catalina/localhost/_/WEB-INF/ProjectFiles

but the thrown exception is always 但抛出的异常总是如此

java.io.FileNotFoundException: (#path) (No such file or directory) java.io.FileNotFoundException:(#path)(没有这样的文件或目录)

What can I do for this? 我能为此做些什么?

Say your file is in the following location: 假设您的文件位于以下位置:

/WEB-INF/ProjectFiles/Risultati/risultat_text_file.txt

Then using: 然后使用:

String path = "/WEB-INF/ProjectFiles/Risultati/risultat_text_file.txt";
InputStream inputStream =  new FileInputStream(this.getServletConfig().getServletContext().getRealPath(path));

Should work for you. 应该适合你。 Note that, ServletContext.getRealPath() return the real OS path corresponding to the given virtual path. 请注意, ServletContext.getRealPath()返回与给定虚拟路径对应的实际OS路径。

Edit: 编辑:

If this doesn't work for your case, you really need to revisit your virtual path. 如果这不适用于您的情况,您确实需要重新访问您的虚拟路径。 You can manually check that does this file exist in the expected directory in the war file or you can log the output of the getRealPath() method to examine what's really going on! 您可以手动检查该文件是否存在于war文件的预期目录中,或者您可以记录getRealPath()方法的输出以检查实际发生的情况! If necessary you can put "/" in your getRealPath() method and examine what is your application's root path. 如有必要,可以在getRealPath()方法中添加"/" ,并检查应用程序的根路径。

Since I don't get application's root realpath, I resolved in this way: 由于我没有得到应用程序的根目录,我以这种方式解决了:

String path="/WEB-INF/ProjectFiles/Risultati/risultati_test.txt";
InputStream inputStream = this.getServletConfig().getServletContext().getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

and now it works. 现在它的工作原理。 By the way, I also found useful informations here 顺便说一下,我在这里也找到了有用的信息

getResourceAsStream() vs FileInputStream getResourceAsStream()vs FileInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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