从servlet访问文件时的FileNotFoundException

时间:2016-08-24 19:07:01

标签: java servlets jboss

尝试从servlet中包含的对象访问文件时,我收到FileNotFoundException。该文件位于WEB-INF / classes目录中。 war文件已部署到JBOSS 6.3。

我尝试了各种方法来访问该文件:

1)InputStream path = new FileInputStream();

2)ClassLoader cLoader = this.getClass()。getClassLoader();     InputStream path = cLoader.getResourceAsStream();

我是否需要在任何JBOSS配置文件中列出文件名或类名?

当我在上面运行1)时,我得到了FileNotFoundException。

当我在上面运行2)时,我得到一个ClassNotFoundException。从servlet我可以找到类,但不能从类本身找到。

感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:-1)

我首先使用ServletContext对象检查默认路径指向的位置。

String realContextPath = getServletContext().getRealPath("");
System.out.println("REAL CONTEXT PATH: " + realContextPath);

然后确保您构建的路径实际上是正确的路径,我的设置已包含/ WEB-INF目录。

File file = new File(realContextPath+"/js/login.js");