在servlet中找不到文件,即使通过context.getRealPath获取路径? [英] File not being found in servlet, even though obtaining path via context.getRealPath?
问题描述
ServletContext context = getServletContext();
String dataFilePath = context.getRealPath(/ WEB-INF / dir / dataFile.txt);
文件dataFile =新文件(dataFilePath);
TestCaseGenerator testCaseGenerator = new TestCaseGenerator(dataFile);
testCaseGenerator.generateTestCase();
TestCaseGenerator
该项目的 war / WEB-INF / lib
文件夹已经作为外部库添加到GWT项目中。
$ b
问题
$ b $ < testCaseGenerator.generateTestCase()
执行时,无法使用 dataFile
创建一个新LineNumberReader(new FileReader(dataFile));
,a FileNotFoundException
被抛出。
我已经验证了 我不确定为什么 预先感谢您的任何见解。 我愚蠢地忽略了包含 我也误解了 获取与给定虚拟路径对应的实际路径。例如,如果path等于/index.html,则此方法将返回服务器文件系统上的 即使 Given the following (abridged) code that lives server-side on a servlet (Tomcat is the container). This is a GWT application, though that should be irrelevant (I think). When I've verified that the I'm not sure why Thanks in advance for any insight. I foolishly neglected to include the And I also misunderstood what Gets the real path corresponding to the given virtual path. For
example, if path is equal to /index.html, this method will return the
absolute file path on the server's filesystem to which a request of
the form http://://index.html would be
mapped, where corresponds to the context path of this
ServletContext. So even though 这篇关于在servlet中找不到文件,即使通过context.getRealPath获取路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! String
c $ c $ dataFilePath 的值是正确的,并包含我需要读取的服务器上的文件的正确路径,以及 dataFile
不是
null
。我也证实了 TestCaseGenerator
在从GWT应用程序之外的命令行调用时可以正常运行。
TestCaseGenerator
不能使用 File
对象我通过它,考虑到我通过它的文件的真正的文件路径。我可以想出一些替代解决方案来解决这个问题,但现在我真的很好奇,为什么它无法找到该文件。
已解决
dir / dataFile.txt
在我的 build.xml中的
war
目标,所以实际上这个文件并没有被包含在 war
包中,因此永远不会放在Tomcat容器中。
< include name =dir / **/>
context.getRealPath(/ WEB-INF / dir / dataFile.txt);
实际返回;从 ServletContext javadoc
绝对文件路径,请求
的格式为http://:/ /index.html将是
映射,其中对应于
ServletContext的上下文路径。
getRealPath
返回一个路径值,但并不一定意味着该文件位于该实际路径中。 Synopsis
ServletContext context = getServletContext();
String dataFilePath= context.getRealPath("/WEB-INF/dir/dataFile.txt");
File dataFile = new File(dataFilePath);
TestCaseGenerator testCaseGenerator = new TestCaseGenerator(dataFile);
testCaseGenerator.generateTestCase();
TestCaseGenerator
is a class from a jar in the project's war/WEB-INF/lib
folder, that's been added to the GWT project as an external library. The Problem
testCaseGenerator.generateTestCase()
gets executed, it's unable to use dataFile
to create a new LineNumberReader(new FileReader(dataFile));
, a FileNotFoundException
gets thrown.String
value of dataFilePath
is correct and contains the proper real path to the file on the server I need to read from, as well that dataFile
isn't null
. I've also verified that TestCaseGenerator
runs just fine when called from a command line, outside of this GWT application. TestCaseGenerator
isn't able to use the File
object I pass it, considering I'm passing it the real file path of the file. I can come up with some alternative solutions to get around this issue, but now I'm genuinely curious why it isn't able to find the file. Solved
dir/dataFile.txt
in my build.xml
's war
target, so in fact the file wasn't being included in the war
package, and thus never placed within the Tomcat container. <include name="dir/**" />
context.getRealPath("/WEB-INF/dir/dataFile.txt");
actual returns;. From ServletContext javadoc
getRealPath
returns a path value, it doesn't necessarily mean the file is at that actual path.