Java Servlet相对路径“;“未找到路径”;
我正在尝试将文件保存到我的文件夹“ProfileImages”,该文件夹位于WebContent中/ 当我以下面的路径运行时,我得到的文件未找到异常Java Servlet相对路径“;“未找到路径”;,java,Java,我正在尝试将文件保存到我的文件夹“ProfileImages”,该文件夹位于WebContent中/ 当我以下面的路径运行时,我得到的文件未找到异常 String appPath = request.getServletContext().getRealPath("/"); String savePath = appPath + "WebContent" + File.separator + "ProfileImages"; System.out.println(appPath + "Web
String appPath = request.getServletContext().getRealPath("/");
String savePath = appPath + "WebContent" + File.separator + "ProfileImages";
System.out.println(appPath + "WebContent" + File.separator + "ProfileImages");
印刷品提供:
C:\Users\me\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\Test\WebContent\ProfileImages\image.jpg (path not found)
无需将“WebContent”文件夹放入保存路径,因为将“/”传递给getRealPath()将返回项目扩展WAR文件的/web文件夹的绝对磁盘文件系统路径
String savePath = appPath + File.separator + "ProfileImages";
另见
谢谢,我将在测试时坚持使用绝对路径。但当我把它作为战争部署时,它起了作用。