在servlet中找不到文件,即使通过context.getRealPath获取路径? [英] File not being found in servlet, even though obtaining path via context.getRealPath?

查看:858
本文介绍了在servlet中找不到文件,即使通过context.getRealPath获取路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定服务器端在servlet上的以下(删节)代码(Tomcat是容器)。这是一个GWT应用程序,虽然这应该是不相关的(我认为)。

  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 被抛出。



我已经验证了 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


获取与给定虚拟路径对应的实际路径。例如,如果path等于/index.html,则此方法将返回服务器文件系统上的
绝对文件路径,请求
的格式为http://:/ /index.html将是
映射,其中对应于
ServletContext的上下文路径。

即使 getRealPath 返回一个路径值,但并不一定意味着该文件位于该实际路径中。

Synopsis

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).

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

When testCaseGenerator.generateTestCase() gets executed, it's unable to use dataFile to create a new LineNumberReader(new FileReader(dataFile));, a FileNotFoundException gets thrown.

I've verified that the 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.

I'm not sure why 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.

Thanks in advance for any insight.

解决方案

Solved

I foolishly neglected to include the 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/**" />

And I also misunderstood what context.getRealPath("/WEB-INF/dir/dataFile.txt"); actual returns;. From ServletContext javadoc

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 getRealPath returns a path value, it doesn't necessarily mean the file is at that actual path.

这篇关于在servlet中找不到文件,即使通过context.getRealPath获取路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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