【发布时间】:2011-04-22 19:10:20
【问题描述】:
在本地 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,结果相同。
我错过了什么?提前致谢。
关于您的所有答案(谢谢),我尽我所能编辑我的问题,结果相同。
一)
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
【问题讨论】: