我正在尝试将文件保存到我的文件夹'ProfileImages',该文件夹位于WebContent /
中当我使用以下路径运行时,我发现文件未找到异常
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)
答案 0 :(得分:1)
没有必要把#Con; WebContent"保存路径中的文件夹,传递" /" getRealPath()会返回项目展开的WAR文件的/ web文件夹的绝对磁盘文件系统路径
String savePath = appPath + File.separator + "ProfileImages";
另见
What does servletcontext.getRealPath(“/”) mean and when should I use it