从类路径加载资源时出现问题

发布于 2024-09-26 07:16:29 字数 1055 浏览 2 评论 0原文


在本地 tomcat 中运行的 Web 应用程序中,我尝试加载位于 tomcat/webapps/myproject/WEB-INF/folder 中的文件夹 /folder 要做到这一点:

InputStream realPath = getClass().getClassLoader().getResourceAsStream("/folder");  

返回null。这段代码应该从类路径加载资源,如果我的文件夹所在的路径没有错误的话。
无论如何,我将文件夹移动到不同的路径,例如 tomcat/webapps/myproject/WEB-INF/classes/folder 或 tomcat/webapps/myproject/WEB-INF/lib/folder< /code> 具有相同的结果。
我错过了什么吗?提前致谢。

关于您的所有答案(谢谢),我用我所尝试的一切编辑了我的问题,但结果相同。
A)

 String realSource = getServletContext().getRealPath("/folder");  

B)

InputStream realPath = getClass().getClassLoader().getResourceAsStream("/folder/fileInFolder"); 

C)

ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
String realSource = servletContext.getRealPath("/folder");

我必须说我的 folder 路径是 tomcat/webapps/myproject/WEB-INF/classes/folder

In a web application running in a local tomcat, I am trying to load a folder /folder which is in tomcat/webapps/myproject/WEB-INF/folder
To do it:

InputStream realPath = getClass().getClassLoader().getResourceAsStream("/folder");  

Which returns null. This piece of code is supposed to load resources from classpath, which is if I am not wrong in the path where my folder is in.
Anyway, I moved my folder to different paths, such as tomcat/webapps/myproject/WEB-INF/classes/folder or tomcat/webapps/myproject/WEB-INF/lib/folder with the same result.
Did I miss something? Thanks in advance.

Regarding on all your answers (thanks), I edit my question with all I have tryed, with the same null result.
A)

 String realSource = getServletContext().getRealPath("/folder");  

B)

InputStream realPath = getClass().getClassLoader().getResourceAsStream("/folder/fileInFolder"); 

C)

ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
String realSource = servletContext.getRealPath("/folder");

I must say that my folder path is tomcat/webapps/myproject/WEB-INF/classes/folder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰つ倒转 2024-10-03 07:16:29

问题是 /WEB-INF 不在 WAR 文件的 CLASSPATH 中。

/WEB-INF/classes 位于 CLASSPATH 中; /WEB-INF/lib 中的所有 JAR 也是如此。

如果您将需要的文件放入 /WEB-INF/classes 中,WAR 类加载器应该能够找到它。

您无法以这种方式读取目录。它必须是一个文件。

更新:

无论如何,我将文件夹移动到了不同的位置
路径,例如
tomcat/webapps/myproject/WEB-INF/classes/文件夹
或者
tomcat/webapps/myproject/WEB-INF/lib/文件夹
得到相同的结果。我错过了吗
某物?

是的,您错过了两件事:

  1. 您的文件夹应该放在一个位置 - /WEB-INF/classes 下
  2. 您无法使用 getResourceAsStream() 访问该文件夹并读取所有内容。事实并非如此。这样您只能读取一个单独的文件。

The problem is that /WEB-INF is not in the CLASSPATH of a WAR file.

/WEB-INF/classes is in the CLASSPATH; so are all the JARs in /WEB-INF/lib.

If you put the file you need into /WEB-INF/classes the WAR class loader should be able to find it.

You cannot read a directory that way. It must be a file.

UPDATE:

Anyway, I moved my folder to different
paths, such as
tomcat/webapps/myproject/WEB-INF/classes/folder
or
tomcat/webapps/myproject/WEB-INF/lib/folder
with the same result. Did I miss
something?

Yes, you've missed two things:

  1. Your folder should go in one place - under /WEB-INF/classes
  2. You cannot access the folder using getResourceAsStream() and read all the contents. It doesn't work that way. You can only read one individual file that way.
我们的影子 2024-10-03 07:16:29

如果您想要 Web 应用程序中文件夹的完整路径,以便可以对其使用文件操作,可以使用以下方法。

注意:myfolder 与我的 web 应用程序中的 WEB-INF 并行

String relativeWebPath = "/myfolder";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);

现在打印 absoluteDiskPath 让我

D:\apache-tomcat-6.0.20\webapps\mywebapp\myfolder

尝试通过以下方式列出该文件夹中的文件

File dir = new File(absoluteDiskPath); 

String[] children = dir.list(); 
if (children != null) 
{ 

    for (int i=0; i<children.length; i++) { 
    // Get filename of file or directory 

    String filename = children[i]; 
    System.out.println("filename no " + i + filename);
    } 
}

If you want the full path of the folder within your webapp so that you can use File operations on it, here is a way to do it.

Note: myfolder is parallel to WEB-INF within my webapp

String relativeWebPath = "/myfolder";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);

Now printing out absoluteDiskPath gives me

D:\apache-tomcat-6.0.20\webapps\mywebapp\myfolder

Try listing files in that folder by

File dir = new File(absoluteDiskPath); 

String[] children = dir.list(); 
if (children != null) 
{ 

    for (int i=0; i<children.length; i++) { 
    // Get filename of file or directory 

    String filename = children[i]; 
    System.out.println("filename no " + i + filename);
    } 
}
梦里梦着梦中梦 2024-10-03 07:16:29

删除前导斜杠并尝试 ie

InputStream realPath = getClass().getClassLoader().getResourceAsStream("folder");  

Remove the leading slash and try i.e.

InputStream realPath = getClass().getClassLoader().getResourceAsStream("folder");  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文