我的应用程序在wtpwebapps文件夹下

wtpwebapps->myapp


我的文件在wtpwebapps-> files文件夹下

wtpwebapps->files->file1
wtpwebapps->files->file2
wtpwebapps->files->file3


我试过了

request.getSession().getServletContext().getRealPath();


并得到了这条路

workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps


当我追加

"\files\file1"


到路径并访问该文件,然后给我错误

The requested resource is not available


如何解决这个问题?

最佳答案

您可以通过以下方式获取myApp/WEB-INF/classes目录的绝对路径:

URL resource = getClass().getResource("/");
String path = resource.getPath();


现在,您可以操纵path字符串转到文件:

path = path.replace("your_app_name/WEB-INF/classes/", "");
path = path + "files/file1";


现在,此处path指的是file1中存在的wtpwebapps->files

10-06 13:37