简体   繁体   English

jsp web应用程序的相对路径

[英]jsp web application relative path

I want to open a file in Java Class in an AWS Java Web project via Eclipse. 我想通过Eclipse在AWS Java Web项目中的Java类中打开一个文件。

I have my file in a folder called "res" in 我将文件放在名为“res”的文件夹中

I tried this 我试过这个

BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("res\\txtFile.txt"), "UTF-8"));

but not working! 但不工作!

I got 我有

java.io.FileNotFoundException: res\\txtFile.txt (The system cannot find the path specified) java.io.FileNotFoundException:res \\ txtFile.txt(系统找不到指定的路径)

If the file is inside the weapp, you want ServletContext.getResourceAsStream or Class.getResourceAsStream. 如果文件在weapp中,则需要ServletContext.getResourceAsStream或Class.getResourceAsStream。 If it is somewhere else on the filesystem you should probably use an absolute path. 如果它在文件系统的其他位置,您应该使用绝对路径。 A relative path like you used is resolved relative to the directory your appserver started from which might not be what you want. 您使用的相对路径是相对于您的appserver启动的目录而解决的,这可能不是您想要的。

I solved it !! 我解决了!!

used this code to get the absolute path of project anywhere 使用此代码获取项目的绝对路径

  String AbsolutePath = new File("").getAbsolutePath();

then add the relative path you need. 然后添加您需要的相对路径。

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

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