Servlet 相对路径"Path not found"



我试图保存文件到我的文件夹'ProfileImages'驻留在WebContent/

当我使用下面的路径运行时,我得到文件未发现异常

String appPath = request.getServletContext().getRealPath("/");
String savePath = appPath + "WebContent" + File.separator + "ProfileImages";
System.out.println(appPath + "WebContent" + File.separator + "ProfileImages");

打印给:

C:Usersmeworkspace.metadata.pluginsorg.eclipse.wst.server.coretmp1wtpwebappsTestWebContentProfileImagesimage.jpg (path not found)

不需要将"WebContent"文件夹放在保存路径中,因为将"/"传递给getRealPath()将返回项目展开的WAR文件的/web文件夹的绝对磁盘文件系统路径

String savePath = appPath +  File.separator + "ProfileImages";

参见

servletcontext.getRealPath("/")是什么意思,我什么时候应该使用它

最新更新