问题描述
我试图在webapp中加载一个文件,当我使用 FileInputStream
时,得到了一个 FileNotFound
>。但是,使用相同的路径,我能够加载文件,当我做 getResourceAsStream()
。
这两个方法有什么区别,为什么一个工作而另一个不工作?
和配置文件在本地磁盘文件系统上运行。问题的根本原因是 java.io
中的相对路径依赖于当前的工作目录。即从这个目录启动JVM(在你的情况下:Web服务器的目录)。例如,这可能是 C:\ Tomcat \ bin
或者完全不同的东西,但是不是 C:\\ \\ Tomcat \webapps\contextname
或任何你所期望的。在一个正常的Eclipse项目中,这将是 C:您可以通过以下方式了解当前工作目录:
System.out.println(新文件(。)。 getAbsolutePath());
但是,工作目录并不是以编程方式控制的。您应该更喜欢在 您不想硬编码或猜测Java(web)应用程序的绝对路径。这只是可移植性的麻烦(即它在系统X中运行,而不在系统Y中运行)。通常的做法是将这些资源放在类路径中,或者将它的完整路径添加到类路径中(在像Eclipse那样的 webapps中的另一个选择是及其对应部分。它能够访问位于webapp项目的public I was trying to load a file in a webapp, and I was getting a The However, the working directory is in no way programmatically controllable. You should really prefer using absolute paths in the You don't want to hardcode or guess the absolute path in Java (web)applications. That's only portability trouble (i.e. it runs in system X, but not in system Y). The normal practice is to place those kind of resources in the classpath, or to add its full path to the classpath (in an IDE like Eclipse that's the Another alternative in webapps is the 这篇关于getResourceAsStream()与FileInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! File
API中使用绝对路径,而不是相对路径。例如。 C:\full\path\to\file.ext
。
src
文件夹和构建路径)。这样,你可以通过 ClassLoader
的帮助来抓取它们。或。它可以找到相对于类路径的根的文件,正如你巧合的想通了一样。在web应用程序(或任何其他使用多个类加载器的应用程序)中,建议使用由 Thread.currentThread()返回的
ClassLoader
。getContextClassLoader / code>,这样你就可以看到webapp上下文的外部。
web
文件夹中的文件,包括 / WEB-INF
文件夹。通过继承方法,您可以按原样调用它。
另请参阅:
FileNotFound
exception when I used FileInputStream
. However, using the same path, I was able to load the file when I did getResourceAsStream()
.What is the difference between the two methods, and why does one work while the other doesn't?java.io.File
and consorts acts on the local disk file system. The root cause of your problem is that relative paths in java.io
are dependent on the current working directory. I.e. the directory from which the JVM (in your case: the webserver's one) is started. This may for example be C:\Tomcat\bin
or something entirely different, but thus not C:\Tomcat\webapps\contextname
or whatever you'd expect it to be. In a normal Eclipse project, that would be C:\Eclipse\workspace\projectname
. You can learn about the current working directory the following way:System.out.println(new File(".").getAbsolutePath());
File
API instead of relative paths. E.g. C:\full\path\to\file.ext
. src
folder and the "build path" respectively). This way you can grab them with help of the ClassLoader
by ClassLoader#getResource()
or ClassLoader#getResourceAsStream()
. It is able to locate files relative to the "root" of the classpath, as you by coincidence figured out. In webapplications (or any other application which uses multiple classloaders) it's recommend to use the ClassLoader
as returned by Thread.currentThread().getContextClassLoader()
for this so you can look "outside" the webapp context as well.ServletContext#getResource()
and its counterpart ServletContext#getResourceAsStream()
. It is able to access files located in the public web
folder of the webapp project, including the /WEB-INF
folder. The ServletContext
is available in servlets by the inherited getServletContext()
method, you can call it as-is. See also: